Swift-确定Array1是否至少包含Array2中的一个对象 [英] Swift - Determine if Array1 contains at least one object from Array2
问题描述
我有2个数组.说array1 = [1,2,3,4,5]
和array2 = [2,3]
.如果array1
至少包含array2
个项目,如何快速检入?
I have 2 Arrays. Say, array1 = [1,2,3,4,5]
and array2 = [2,3]
. How could I check in swift if array1
contains at least one item from array2
?
推荐答案
You can do this by simply passing in your array2
's contains
function into your array1
's contains
function (or vice versa), as your elements are Equatable
.
let array1 = [2, 3, 4, 5]
let array2 = [20, 15, 2, 7]
// this is just shorthand for array1.contains(where: { array2.contains($0) })
if array1.contains(where: array2.contains) {
print("Array 1 and array 2 share at least one common element")
} else {
print("Array 1 doesn't contains any elements from array 2")
}
这通过遍历数组1的元素来工作.然后,对于每个元素,它将遍历数组2以检查它是否存在于该数组中.如果找到该元素,它将中断并返回true-否则返回false.
This works by looping through array 1's elements. For each element, it will then loop through array 2 to check if it exists in that array. If it finds that element, it will break and return true – else false.
之所以起作用,是因为实际上有两种contains
口味.一个使用闭包以根据自定义谓词检查每个元素,而另一个仅直接比较一个元素.在此示例中,array1
使用的是元素版本.这就是您可以将contains
函数传递给另一个contains
函数的原因.
This works because there are actually two flavours of contains
. One takes a closure in order to check each element against a custom predicate, and the other just compares an element directly. In this example, array1
is using the closure version, and array2
is using the element version. And that is the reason you can pass a contains
function into another contains
function.
尽管如此,如正确指向由@AMomchilov 得出,上述算法为O(n 2 ).一个好的集合交集算法是O(n),因为元素查找是O(1).因此,如果您的代码对性能至关重要,则绝对应该使用集(如果您的元素为Hashable
), simpleBob .
Although, as correctly pointed out by @AMomchilov, the above algorithm is O(n2). A good set intersection algorithm is O(n), as element lookup is O(1). Therefore if your code is performance critical, you should definitely use sets to do this (if your elements are Hashable
), as shown by @simpleBob.
尽管您想利用contains
给您带来的提前退出的机会,您还是想做些类似的事情:
Although if you want to take advantage of the early exit that contains
gives you, you'll want to do something like this:
extension Sequence where Iterator.Element : Hashable {
func intersects<S : Sequence>(with sequence: S) -> Bool
where S.Iterator.Element == Iterator.Element
{
let sequenceSet = Set(sequence)
return self.contains(where: sequenceSet.contains)
}
}
if array1.intersects(with: array2) {
print("Array 1 and array 2 share at least one common element")
} else {
print("Array 1 doesn't contains any elements from array 2")
}
这与使用数组的contains方法非常相似– arraySet.contains
方法现在为O(1)的事实有很大不同.因此,整个方法现在将以O(n)(其中n是两个序列中的较大长度)运行,并且有可能提前退出.
This works much the same as the using the array's contains method – with the significant difference of the fact that the arraySet.contains
method is now O(1). Therefore the entire method will now run at O(n) (where n is the greater length of the two sequences), with the possibility of exiting early.
这篇关于Swift-确定Array1是否至少包含Array2中的一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!