将一个数组中的所有元素与其他数组进行比较

时间:2021-11-05 21:26:23

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?

假设有三条可变射线arr1 arr2 arr3。我想比较arr1中的所有元素和arr2中的每个元素,如果arr2中的一个元素包含arr1中的所有元素,我想把它添加到arr3中。所以我想它应该像下面的代码一样。objective-c中是否存在我不知道的智能函数?

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

2 个解决方案

#1


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.

查看数组是否包含另一个数组的所有元素的最佳方法是使用nsset。NSSet将是一组不同对象的静态集合,这意味着当您从一个数组创建一个集合时,该集合将只包含数组中每个disctint对象的一个条目。换句话说,一个数组可以有一个对象的多个副本,一个集合将只有每个对象的一个副本。

The important part of using NSSet is the ability to call the isSubsetOfSet method:

使用NSSet的重要部分是调用isSubsetOfSet方法的能力:

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

isSubsetOfSet:返回一个布尔值,该值指示接收集中的每个对象是否也存在于另一个给定集合中。

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

您将需要从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]];
}

#2


2  

Done, in 6 lines of code:

完成,在6行代码中:

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.

因为NSSet可以直接复制NSArray的底层缓冲区,所以就性能而言,这应该是非常有效的。

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

如果你愿意,这也可以很容易地转换成一个类别。

#1


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.

查看数组是否包含另一个数组的所有元素的最佳方法是使用nsset。NSSet将是一组不同对象的静态集合,这意味着当您从一个数组创建一个集合时,该集合将只包含数组中每个disctint对象的一个条目。换句话说,一个数组可以有一个对象的多个副本,一个集合将只有每个对象的一个副本。

The important part of using NSSet is the ability to call the isSubsetOfSet method:

使用NSSet的重要部分是调用isSubsetOfSet方法的能力:

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

isSubsetOfSet:返回一个布尔值,该值指示接收集中的每个对象是否也存在于另一个给定集合中。

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

您将需要从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]];
}

#2


2  

Done, in 6 lines of code:

完成,在6行代码中:

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.

因为NSSet可以直接复制NSArray的底层缓冲区,所以就性能而言,这应该是非常有效的。

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

如果你愿意,这也可以很容易地转换成一个类别。