Let’s say I have three mutablearrays: arr1, arr2 and arr3. I want to compare all the element in arr1 to each element in arr2, and if an element in arr2 contains all the elements in arr1, i want to add it to arr3. So i’m thinking it would look something like my code below. Is there some smart function in objective-c i don’t know about, or any way this could be done?
for(int i; i < arr2.count; i++)
{
if([arr2 objectAtIndex:i] containAllElementsInArray:arr1]])
{
[arr3 addObject:[arr2 objectAtIndex:i]];
}
}
The best way to see if an array contains all the elements of another array is to work with NSSets. An NSSet will be a static set of distinct objects, which means that when you create a set from an array the set will contain only 1 entry for every disctint object in the array. In other words, an array can have multiple copies of an object, a set will have only 1 copy of each object.
The important part of using NSSet is the ability to call the isSubsetOfSet method:
You will need to create a set from arr1 and compare this to each element in arr2, to see if it’s a subset of that element…