Compare the whole element in one array with another array

Say I have three mutablearrays: arr1, arr2 and arr3. I want to compare the whole element in arr1 with every element in arr2, and if the element in arr2 contains all the elements in arr1, I want to add it to arr3. So I think it will look like my code below. Is there some kind of smart function in objective-c that I don't know about, or can it be done in any way?

for(int i; i < arr2.count; i++)
{
  if([arr2 objectAtIndex:i] containAllElementsInArray:arr1]])
  {
    [arr3 addObject:[arr2 objectAtIndex:i]];
  }
}
+3
source share
2 answers

, , - NSSets. NSSet , , 1 - . , , 1 .

NSSet isSubsetOfSet:

isSubsetOfSet: , , .

- (BOOL)isSubsetOfSet:(NSSet *)otherSet

arr1 arr2, , ...

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]];
}
+8

, 6 :

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

    return [resultSet allObjects];
}

NSSet NSArray, , .

, .

+2

All Articles