Last active
December 29, 2015 17:09
-
-
Save sag333ar/7702510 to your computer and use it in GitHub Desktop.
Set operations in Objective-C
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
// SET A - SET B ------------------------ | |
// an array that contains many objects | |
NSMutableArray *arForAll = [NSMutableArray arrayWithObjects:@"1",@"2",@"Nimit",@"sagar",@"3", nil]; | |
// an array which is sort of subset of above array. | |
NSMutableArray *selForSelected = [NSMutableArray arrayWithObjects:@"Nimit",@"sagar", nil]; | |
// create a set with array - major set. | |
NSMutableSet* set1 = [NSMutableSet setWithArray:arForAll]; | |
// create small set with array | |
NSMutableSet* set2 = [NSMutableSet setWithArray:selForSelected]; | |
// set1 - set2 | |
[set1 minusSet:set2]; | |
// result goes here. | |
NSArray* result = [set1 allObjects]; | |
NSLog(@"%@",result); | |
/* | |
Result should be For Minus Set. | |
-------------------------- | |
( | |
3, | |
1, | |
2 | |
) | |
-------------------------- | |
*/ | |
// ----------------------------------------------------------------------------------------- | |
// SET A intersects SET B ------------------------ | |
NSMutableArray *arForAll = [NSMutableArray arrayWithObjects:@"1",@"2",@"Nimit",@"sagar",@"3", nil]; | |
NSMutableArray *selForSelected = [NSMutableArray arrayWithObjects:@"Nimit",@"sagar", nil]; | |
NSMutableSet* set1 = [NSMutableSet setWithArray:arForAll]; | |
NSMutableSet* set2 = [NSMutableSet setWithArray:selForSelected]; | |
[set1 intersectSet:set2]; | |
NSArray* result = [set1 allObjects]; | |
NSLog(@"%@",result); | |
/* | |
Result should be For intersect Set. | |
-------------------------- | |
( | |
sagar, | |
Nimit | |
) | |
-------------------------- | |
*/ | |
// ----------------------------------------------------------------------------------------- | |
// SET A union SET B ------------------------ | |
NSMutableArray *arForAll = [NSMutableArray arrayWithObjects:@"1",@"2",@"Nimit",@"sagar",@"3", nil]; | |
NSMutableArray *selForSelected = [NSMutableArray arrayWithObjects:@"Nimit",@"sagar", nil]; | |
NSMutableSet* set1 = [NSMutableSet setWithArray:arForAll]; | |
NSMutableSet* set2 = [NSMutableSet setWithArray:selForSelected]; | |
[set1 unionSet:set2]; | |
NSArray* result = [set1 allObjects]; | |
NSLog(@"%@",result); | |
/* | |
Result should be For unionSet Set. | |
-------------------------- | |
( | |
3, | |
1, | |
sagar, | |
2, | |
Nimit | |
) | |
-------------------------- | |
*/ | |
// ----------------------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment