3

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]];
  }
}
5
  • 1
    In other words, Array3 should contain the intersection of Array1 and Array2, correct? Commented Apr 23, 2012 at 17:02
  • 1
    I don't see arr1 anywhere in your code. Still trying to figure out if you want an intersection or another behaviour (ie: "if an element in arr2 contains all the elements in arr1") Commented Apr 23, 2012 at 17:23
  • As I understand your logic as you posted above, you have an Arr2 whose elements each contain arrays of objects. And, you want to know explicitly if individual elements in Arr2 contains all the objects in Arr1? That is, you're only interested to know if [Arr2 objectAtIndex:i] contains 5/5 elements from Arr1, if it only contains 4/5 elements you don't care...? Commented Apr 23, 2012 at 17:42
  • @C4-Travis yes, that's exactly what i want. I don't believe the other two methods provided will do that for me, sadly. Commented Apr 23, 2012 at 17:44
  • i think i have a solution though, just working on the answer... Commented Apr 23, 2012 at 17:45

2 Answers 2

8

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:

isSubsetOfSet: Returns a Boolean value that indicates whether every object in the receiving set is also present in another given set.

- (BOOL)isSubsetOfSet:(NSSet *)otherSet

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...

NSSet *arr1set = [NSSet setWithArray:arr1];
NSSet *arr2set = [NSSet setWithArray:[arr2 objectAtIndex:i]];

if ([arr1set isSubsetOfSet:arr2set]) {
    // then the element [arr2 objectAtIndex:i] contains all the elements of arr1
    [arr3 addObject:[arr2 objectAtIndex:i]];
}
Sign up to request clarification or add additional context in comments.

1 Comment

It seems this answer matches exactly what the OP asked for. Kudos to you for deciphering it :D
2

Done, in 6 lines of code:

NSArray *intersectArray(NSArray *arr1, NSArray *arr2)
{
    NSMutableSet *resultSet = [NSMutableSet setWithArray:arr1];
    [resultSet intersectSet:[NSSet setWithArray:arr2]];

    return [resultSet allObjects];
}

Because NSSet can directly copy the underlying buffer of a NSArray, this should be very effective as terms of performance go.

This could also be very easily converted into a category, if you'd like.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.