Skip to content

Instantly share code, notes, and snippets.

@jeffreyjackson
Created April 8, 2015 14:36
Show Gist options
  • Save jeffreyjackson/bfbc8f68e4cb1a201fd9 to your computer and use it in GitHub Desktop.
Save jeffreyjackson/bfbc8f68e4cb1a201fd9 to your computer and use it in GitHub Desktop.
Merging 2 NSArrays & Duplicates
//You have two NSArrays and you want to merge them into 1 Array.
//Easy. You need to know about NSSet and maybe a little 'Set Theory'.
NSMutableSet *mergedSet = [NSMutableSet setWithArray:arrayFirst];
[mergedSet unionSet:[NSSet setWithArray:arraySecond]];
//In Set Theory, a union will automatically remove your duplicates. In the case you only wanted to know about duplicates, then take a look at intersectSet:
//Take your new set one step further by sorting them alphabetically with a block:
arraySorted = [[mergedSet allObjects] sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSString *first = [a objectForKey:@"identifier"];
NSString *second = [b objectForKey:@"identifier"];
return [first compare:second];
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment