你如何比较两个数组来获得类似对象的数量?

时间:2021-12-22 18:47:06

I have two string arrays with unique amounts of content and data in each.

我有两个字符串数组,每个数组都有独特的内容和数据量。

I want to be able to find the count of the number of items that appear in both arrays.

我希望能够找到两个数组中出现的项目数。

Example:

例:

var array1 = ["Duck", "Dog", "Cat", "Bird", "Elephant", "Cow", "Goat", "Goose"]
var array2 = ["Eagle", "Giraffe", "Cow", "Elephant", "Sheep", "Penguin", "Rhinoceros"]

This should print 2, because Cow and Elephant appear in both array1 and array2.

这应该打印2,因为Cow和Elephant出现在array1和array2中。

My progress is below. This is throwing an error: Closure tuple parameter '(offset: Int, element: (String, String))' does not support destructuring with implicit parameters

我的进展如下。抛出错误:Closure元组参数'(offset:Int,element:(String,String))'不支持使用隐式参数进行解构

let compared = zip(array1, array2).enumerated().filter() {
    $1.0.id == $1.1.id
}.count

print(compared)

How do I find the count of items that appear in both arrays? Note, there will never be 3 or more arrays. Always will compare 2 arrays.

如何查找两个阵列中出现的项目数?注意,永远不会有3个或更多阵列。总是会比较2个阵列。

2 个解决方案

#1


6  

Maybe you can use Set operation:

也许你可以使用Set操作:

var array1 = ["Duck", "Dog", "Cat", "Bird", "Elephant", "Cow", "Goat", "Goose"]
var array2 = ["Eagle", "Giraffe", "Cow", "Elephant", "Sheep", "Penguin", "Rhinoceros"]

print( Set(array1).intersection(array2).count ) //-> 2

#2


1  

You can create a generic function that returns the common elements of two arrays by returning the intersection of the two Sets created from the Arrays. The Hashable generic type restriction is needed since elements of a Set need to conform to Hashable.

您可以创建一个泛型函数,通过返回从数组创建的两个集合的交集来返回两个数组的公共元素。由于Set的元素需要符合Hashable,因此需要Hashable泛型类型限制。

func commonElements<T:Hashable>(between array1:[T],and array2:[T])->[T]{
    return Array(Set(array1).intersection(Set(array2)))
}

commonElements(between: array1, and: array2) // ["Cow", "Elephant"]

If you are only interested in the number of such elements, you can simply call count on the return value.

如果您只对这些元素的数量感兴趣,则可以简单地在返回值上调用count。

commonElements(between: array1, and: array2).count // 2

#1


6  

Maybe you can use Set operation:

也许你可以使用Set操作:

var array1 = ["Duck", "Dog", "Cat", "Bird", "Elephant", "Cow", "Goat", "Goose"]
var array2 = ["Eagle", "Giraffe", "Cow", "Elephant", "Sheep", "Penguin", "Rhinoceros"]

print( Set(array1).intersection(array2).count ) //-> 2

#2


1  

You can create a generic function that returns the common elements of two arrays by returning the intersection of the two Sets created from the Arrays. The Hashable generic type restriction is needed since elements of a Set need to conform to Hashable.

您可以创建一个泛型函数,通过返回从数组创建的两个集合的交集来返回两个数组的公共元素。由于Set的元素需要符合Hashable,因此需要Hashable泛型类型限制。

func commonElements<T:Hashable>(between array1:[T],and array2:[T])->[T]{
    return Array(Set(array1).intersection(Set(array2)))
}

commonElements(between: array1, and: array2) // ["Cow", "Elephant"]

If you are only interested in the number of such elements, you can simply call count on the return value.

如果您只对这些元素的数量感兴趣,则可以简单地在返回值上调用count。

commonElements(between: array1, and: array2).count // 2