Several ways to remove duplicate data from an array in iOS

Source: Internet
Author: User
Tags allkeys

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.