Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check that the contents of one NSArray are all in another array

I have one NSArray with names in string objects like this:@[@"john", @"smith", @"alex", @"louis"], and I have another array that contains lots of names. How can I check that all the objects in the first array are in the second?

like image 528
Nassif Avatar asked Mar 07 '13 10:03

Nassif


2 Answers

NSSet has the functionality that you are looking for.

If we disregard performance issues for a moment, then the following snippet will do what you need in a single line of code:

BOOL isSubset = [[NSSet setWithArray: array1] isSubsetOfSet: [NSSet setWithArray: mainArray]];
like image 183
Monolo Avatar answered Sep 18 '22 13:09

Monolo


Use this code..

NSArray *temp1 = [NSArray arrayWithObjects:@"john",@"smith",@"alex",@"loui,@"Jac", nil];
NSArray *temp2 = [NSArray arrayWithObjects:@"john",@"smith",@"alex",@"loui,@"Rob", nil];

NSMutableSet *telephoneSet = [[NSMutableSet alloc] initWithArray:temp1] ;
NSMutableSet *telephoneSet2 = [[NSMutableSet alloc] initWithArray:temp2];


[telephoneSet intersectSet:telephoneSet2];

 NSArray *outPut = [telephoneSet allObjects];
 NSLog(@"%@",outPut);

output array contains:

"john","smith","alex","loui

as per your requirement.

like image 26
Ganapathy Avatar answered Sep 19 '22 13:09

Ganapathy