Reputation: 580
I have 2
NSArray
, lets call it as CountriesArray
and UNCountriesArray
. The CountriesArray
contains all the countries in the world and the UNCountriesArray
contains all the countries that belongs to the united nations.
I Want to get the subset
of the 2 arrays. so finally i should get an array that has the countries that does not belong to the united nations. Can some one help me write the objective-c
code that gets the subset of the 2 arrays?
Upvotes: 2
Views: 828
Reputation: 10083
You can use the removeObjectsInArray method of NSMutableArray to do this. For example:
NSMutableArray *countriesArray = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
NSArray *unCountriesArray = [NSArray arrayWithObjects:@"2", @"4", nil];
[countriesArray removeObjectsInArray:unCountriesArray];
NSLog(@"Countries array: %@", countriesArray);
Upvotes: 1
Reputation: 135548
Use sets:
NSMutableSet *countriesSet = [NSMutableSet setWithArray:countriesArray];
NSSet *unSet = [NSSet setWithArray:unCountriesArray];
[countriesSet minusSet:unSet];
// countriesSet now contains only those countries who are not part of unSet
Keep in mind that the members of the set are unsorted. If you want to have a sorted array, you will have to re-sort the result.
Upvotes: 3