Nsarray Removing duplicate elements

Source: Internet
Author: User

Go directly to the code! 1. You can create a new array that iterates over the array that needs to be removed, and if the new array does not contain an array, add the element if it is included.
1Nsmutablearray *array = [Nsmutablearray arraywithobjects:@"1",@"2",@"2",@"4",@"5",@"6",@"7",@"8",@"9",@"0", nil];2    3Nsmutablearray *New=[Nsmutablearray array];4      for(inti =0; I<[array Count]; i++) {5         6         if([NewContainsobject:[array Objectatindex:i]] = =NO) {7[NewAddobject:[array Objectatindex:i]];8         }9     }TenNSLog (@"%@",New);

Print Result: (1,
2,
4,
5,
6,
7,
8,
9,
0) 2. You can also use Nsset to go to the weight, but this method cannot be reordered, so the de-weight result is confusing code as follows
Nsmutablearray *array = [Nsmutablearray arraywithobjects:@"1",@"2",@"2",@"4",@"5",@"6",@"7",@"8",@"9",@"0", nil]; Nsset*Set=[Nsset Setwitharray:array]; NSLog (@"%@",[Setallobjects]); 

Printing results: (
7,
8,
4,
0,
9,
5,
1,
6,
2)

Nsarray Removing duplicate elements

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.