Created
October 20, 2010 00:36
-
-
Save agoodman/635524 to your computer and use it in GitHub Desktop.
Cocoa Set Intersection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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