Several ways to remove duplicate data from an array in iOS
In the work of the project we do not have to encounter, in the array when there is duplicate data, how to remove the duplicate data?
First: Using Nsdictionary's AllKeys (allvalues) method
You can save the elements in the Nsarray in a dictionary, and then use AllKeys or allvalues to get all the keys or values of the dictionary, which are de-weighed. Code:
Nsarray*dataarray =@[@"2014-14L01",@ "2014-04-02",@"2014-14L03",@ "2014-04-01",@"2014-14L02",@ "2014-04-03",@"2014-14L01",@ "2014-04-03",@"2014-14L03",@ "2014-04-01",@"2014-14L02",@ "2014-04-03",@"2014-04-01",@ "2014-04-02",@"2014-04-03",@ "2014-04-01",@"2014-04-02",@ "2014-04-03", @ "2014- 04-04@" 2014-04-06 ", @ "2014-04-08@ " 2014-04-05 ", @" 2014-04-07 @" 2014-04-09 ",]; Nsmutabledictionary *dic = [[Nsmutabledictionary alloc]initwithcapacity: 0]; for (nsstring *str in DataArray) {[dic setvalue:str forkey:str];} NSLog (@ "%@
Printing results:
( "2014-04-09", "2014-04-02", "2014-04-08", "2014-04-01", "2014-04-07", "2014-04-06", "2014-04-05", "2014-04-04", "2014-04-03")
The second method: Using the AllObjects method of Nsset
This method is faster, and using Nsset does not add the attributes of the repeating element. But the go-to-heavy array is not sorted, and if you need to sort, you can use the Nssortdescriptor class. Instance code:
Nsarray*dataarray =@[@"2014-14L01",@ "2014-04-02",@"2014-14L03",@ "2014-04-01",@"2014-14L02",@ "2014-04-03",@"2014-14L01",@ "2014-04-03",@"2014-14L03",@ "2014-04-01",@"2014-04-02",@ "2014-04-03",@"2014-04-01",@ "2014-04-02",@"2014-04-03",@ "2014-04-01",@ "2014-04-", @ " 2014-04-03", @ "2014-04-",@ "2014-04-06", @ "2014-04-08", @ "2014-04-05",@ "2014-04-",@ "2014-04-09" ,]; Nsset *set = [Nsset Setwitharray:dataarray]; NSLog (@ "%@", [set allobjects]);
Printing results:
( "2014-04-01", "2014-04-02", "2014-04-03", "2014-04-04", "2014-04-06", "2014-04-08", "2014-04-05", "2014-04-07", "2014-04-09")
The third method uses the containsobject of the array to remove
The code is as follows:
Nsarray*dataarray =@[@"2014-04-01",@ "2014-04-02",@"2014-04-03",@ "2014-04-01",@"2014-04-02",@ "2014-04-03",@"2014-04-01",@ "2014-04-03",@"2014-04-03",@ "2014-04-01",@"2014-04-02",@ "2014-04-03",@"2014-04-01",@ "2014-04-02",@"2014-04-03",@ "2014-04-01",@"2014-04-02",@ "2014-04-03",@ "2014-04-",@ "2014-04-06",@ "2014-04-08", @ "2014-04-05", @ " 2014-04-",@" 2014-04-09 ",]; Nsmutablearray *listary = [[Nsmutablearray alloc]init]; For (NSString *str in DataArray) { if (![ Listary Containsobject:str]) {[Listary addobject:str];}} NSLog (@ "%@", listary);
Printing results:
( "2014-04-01", "2014-04-02", "2014-04-03", "2014-04-04", "2014-04-06", "2014-04-08", "2014-04-05", "2014-04-07", "2014-04-09")
The fourth method: Use the KeyValue way (oneself not too understand)
Nsarray*dataarray =@[@"2014-14L01",@ "2014-04-02",@"2014-14L03",@ "2014-04-01",@"2014-14L02",@ "2014-04-03",@"2014-14L01",@ "2014-04-03",@"2014-14L03",@ "2014-04-01",@"2014-14L02",@ "2014-04-03",@"2014-14L01",@ "2014-04-02",@"2014-14L03",@ "2014-04-01",@"2014-04-02@ "2014-04-03", @ "2014- 04-04@" 2014-04-06 ", @ "2014-04-08@ " 2014-04-05 ", @" 2014-04-07 @" 2014-04-09 ",]; dataarray = [DataArray valueforkeypath:@" @distinctUnionOfObjects. Self@ "%@", DataArray);
The printing results are as follows:
( "2014-04-01", "2014-04-02", "2014-04-03", "2014-04-04", "2014-04-06", "2014-04-08", "2014-04-05", "2014-04-07", "2014-04-09" )
Summary: These methods can be implemented to remove duplicate data, of course, can also be used for a loop to remove the method is no longer described, indifferent also refer to other people's things to carry out the summary
Use of the case:
The server returns an array with a date, of course, there are duplicate dates, the same date list to be in the same place, the head display date, you can use this method to remove duplicates, of course, according to the specific situation code and logic is different!
Resource Reference
- http://blog.csdn.net/wm9028/article/details/49926535
Several ways to remove duplicate data from an array in iOS