如何在Swift中获取2个数组的常用元素列表? [英] How to get list of common elements of 2 array in Swift?
问题描述
我有两个数组:
fruitsArray = ["apple", "mango", "blueberry", "orange"]
vegArray = ["tomato", "potato", "mango", "blueberry"]
如何获取这两个数组中的常用项目列表
How can I get the list of common items in those two array which gives
ouptput = ["mango", "blueberry"]
如果包含(数组,字符串)我不能使用
因为我想比较2个数组。
I can't use if contains(array, string)
as I want to compare 2 arrays.
推荐答案
您还可以使用过滤器
和包含
:
let fruitsArray = ["apple", "mango", "blueberry", "orange"]
let vegArray = ["tomato", "potato", "mango", "blueberry"]
// only Swift 1
let output = fruitsArray.filter{ contains(vegArray, $0) }
// in Swift 2 and above
let output = fruitsArray.filter{ vegArray.contains($0) }
// or
let output = fruitsArray.filter(vegArray.contains)
设置
vs 数组
用于公共元素的单次计算
我们考虑以下代码片段:
Set
vs Array
for a single computation of common elements
We consider the following code snippet:
let array1: Array = ...
let array2: Array = ...
// `Array`
let commonElements = array1.filter(array2.contains)
// vs `Set`
let commonElements = Array(Set(array1).intersection(Set(array2)))
// or (performance wise equivalent)
let commonElements: Array = Set(array1).filter(Set(array2).contains)
我用 Int
和短/长字符串
s(10)做了一些(人为的)基准测试到100 字符
s)(所有随机生成的)。我总是使用 array1.count == array2.count
I have made some (artificial) benchmarks with Int
and short/long String
s (10 to 100 Character
s) (all randomly generated). I always use array1.count == array2.count
我得到以下结果:
如果你有超过关键#(元数)元素
转换为设置
更可取
If you have more than critical #(number of) elements
converting to a Set
is preferable
data | critical #elements
-------------|--------------------
Int | ~50
short String | ~100
long String | ~200
结果说明
使用数组
方法使用暴力搜索,其中时间复杂度 O(N ^ 2)
其中 N = array1.count = array2.count
其中与 Set
方法 O(N)
相反。但是,对于大数据,从数组
到 Set
的转换非常昂贵,这解释了<$ c $的增加c>对于更大的数据类型,关键#elements 。
Explanation of the results
Using the Array
approach uses "Brute force"-search which has time complexity O(N^2)
where N = array1.count = array2.count
which is in contrast to the Set
approach O(N)
. However the conversion from Array
to Set
and back is very expensive for large data which explains the increase of critical #elements
for bigger data types.
对于包含大约100个元素的小数组
,数组
方法很好但是对于较大的那些你应该使用 Set
方法。
For small Array
s with about 100 elements the Array
approach is fine but for larger ones you should use the Set
approach.
如果你想使用这个共同元素 - 操作多个如果可能的话,建议使用设置
s (元素的类型必须是 Hashable
)。
If you want to use this "common elements"-operation multiple times it is advisable to use Set
s only if possible (the type of the elements has to be Hashable
).
从数组转换
到设置
从 Set
转换为数组时价格昂贵
相比之下非常便宜。
A conversion from Array
to Set
is kind of expensive while the conversion from Set
to Array
is in contrast very inexpensive.
使用过滤器
与 .filter(array1.contains)
的性能明显快于 .filter {array1.contains($ 0)}
自:
Using filter
with .filter(array1.contains)
is performance wise faster than .filter{ array1.contains($0) }
since:
- 最后一个创建一个新的闭包(只有一次)而第一个只传递一个函数指针
- ,对于最后一个,闭包的调用会创建一个额外的堆栈帧,这会花费空间和时间(多次:
O(N)
)
- the last one creates a new closure (only once) whereas the first one passes only a function pointer
- for the last one the call of the closure creates an additional stack frame which costs space and time (multiple times:
O(N)
)
这篇关于如何在Swift中获取2个数组的常用元素列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!