Swift - 确定 Array1 是否包含来自 Array2 的至少一个对象 [英] Swift - Determine if Array1 contains at least one object from Array2

查看:21
本文介绍了Swift - 确定 Array1 是否包含来自 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?

推荐答案

你可以通过简单地传入你的 array2contains 函数进入你的 array1contains 函数(反之亦然),因为您的元素是 Equatable.

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 使用的是 闭包版本,并且 array2 正在使用 元素版本.这就是您可以将 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(n2).一个好的集合交集算法是 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆