Skip to content

Instantly share code, notes, and snippets.

@agoodman
Created October 20, 2010 00:36
Show Gist options
  • Save agoodman/635524 to your computer and use it in GitHub Desktop.
Save agoodman/635524 to your computer and use it in GitHub Desktop.
Cocoa Set Intersection
NSArray* set1 = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];
NSArray* set2 = [NSArray arrayWithObjects:@"two",@"three",@"four",nil];
NSMutableArray* set3 = [NSMutableArray arrayWithCapacity:MIN(set1.count,set2.count)];
for (NSString* element in set1) {
if( [set2 containsObject:element] ) {
[set3 addObject:element];
}
}
// set3 => [ @"two", @"three" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment