如何在Swift中获取2个数组的不同(非常见)项的列表

时间:2022-05-01 09:02:10

This question is similliar to my other one

这个问题与我的另一个问题类似

I have 2 arrays:

我有2个数组:

fruitsArray = ["apple", "mango", "blueberry", "orange"]
vegArray = ["tomato", "potato", "mango", "blueberry"]

How can I get the uncommon elements of fruitsArray comparing to vegArray

与vegArray相比,如何获得fruitsArray的不常见元素

Expected Output = ["apple", orange]

4 个解决方案

#1


5  

Convert arrays to Set and use subtract() function to get what you want

将数组转换为Set并使用subtract()函数来获得所需的数据

   let fruitsArray = ["apple", "mango", "blueberry", "orange"]
   let vegArray = ["tomato", "potato", "mango", "blueberry"]

   let set1 = Set(fruitsArray)
   let set2 = Set(vegArray)

   let filter = Array(set1.subtract(set2))
   println(filter) //[apple, orange]

#2


2  

One line

let fruitsArray = ["apple", "mango", "blueberry", "orange"]
let vegArray = ["tomato", "potato", "mango", "blueberry"]

let answer = fruitsArray.filter{ item in !vegArray.contains(item) }

print("\(answer)") //  ["apple", "orange"] 

#3


1  

There are actually a couple of difficulties with this operation. First off, there's a difference between the two things you could be asking for: either "difference" or "subtraction". Difference would return the elements that aren't in both arrays, so for instance:

这个操作实际上有几个困难。首先,你可以要求的两件事之间存在差异:“差异”或“减法”。差异将返回不在两个数组中的元素,例如:

let fruitsArray = ["apple", "mango", "blueberry", "orange"]
let vegArray = ["tomato", "potato", "mango", "blueberry"]

difference(fruitsArray, vegArray) // ["apple", "orange", "tomato", "blueberry"]

That doesn't seem to be what you're asking for, but it's worth bearing in mind.

这似乎不是你所要求的,但值得记住。

The second is a "subtraction", which would yield your expected result. There's one more thing to bear in mind about this. The obvious one-liner:

第二个是“减法”,它会产生你的预期结果。还有一件事需要牢记。明显的单线:

fruitsArray.filter{ !vegArray.contains($0) }
// ["apple", "orange"]

Is quadratic. No issue if performance isn't important, but it's easily avoidable, using a Set. Now, you could convert both, and use the normal Set methods, but that's unnecessary. Converting to a Set is O(n) (I think, I'm not actually sure), but you're going to have to iterate through the set from fruitsArray anyway, and convert it back to an array in the end. The only advantage of a Set is that is has an O(1) contains method - so you only need to convert the this that you're using contains on. The most efficient way, I think, is:

是二次的。没有问题,如果性能不重要,但使用Set可以轻松避免。现在,你可以转换它们,并使用普通的Set方法,但这是不必要的。转换为Set是O(n)(我想,我不确定),但是你将不得不从fruitsArray迭代遍历集合,并最终将它转换回数组。 Set的唯一优点是它有一个O(1)contains方法 - 所以你只需要转换你正在使用的包含的方法。我认为最有效的方法是:

let vSet = Set(vegArray)
fruitsArray.filter { m in !vSet.contains(m) }

#4


0  

Powerful functional one-liner:

功能强大的单线程:

// Swift 1.2
let differenceResult = fruitsArray.filter({!contains(vegArray, $0)})

// Swift 2.0
let differenceResult = fruitsArray.filter(!vegArray.contains)

and obviously for your other question, simply remove the !:

显然,对于你的其他问题,只需删除!:

// Swift 1.2
let intersectionResult = fruitsArray.filter({contains(vegArray, $0)})

// Swift 2.0
let intersectionResult = fruitsArray.filter(vegArray.contains)

#1


5  

Convert arrays to Set and use subtract() function to get what you want

将数组转换为Set并使用subtract()函数来获得所需的数据

   let fruitsArray = ["apple", "mango", "blueberry", "orange"]
   let vegArray = ["tomato", "potato", "mango", "blueberry"]

   let set1 = Set(fruitsArray)
   let set2 = Set(vegArray)

   let filter = Array(set1.subtract(set2))
   println(filter) //[apple, orange]

#2


2  

One line

let fruitsArray = ["apple", "mango", "blueberry", "orange"]
let vegArray = ["tomato", "potato", "mango", "blueberry"]

let answer = fruitsArray.filter{ item in !vegArray.contains(item) }

print("\(answer)") //  ["apple", "orange"] 

#3


1  

There are actually a couple of difficulties with this operation. First off, there's a difference between the two things you could be asking for: either "difference" or "subtraction". Difference would return the elements that aren't in both arrays, so for instance:

这个操作实际上有几个困难。首先,你可以要求的两件事之间存在差异:“差异”或“减法”。差异将返回不在两个数组中的元素,例如:

let fruitsArray = ["apple", "mango", "blueberry", "orange"]
let vegArray = ["tomato", "potato", "mango", "blueberry"]

difference(fruitsArray, vegArray) // ["apple", "orange", "tomato", "blueberry"]

That doesn't seem to be what you're asking for, but it's worth bearing in mind.

这似乎不是你所要求的,但值得记住。

The second is a "subtraction", which would yield your expected result. There's one more thing to bear in mind about this. The obvious one-liner:

第二个是“减法”,它会产生你的预期结果。还有一件事需要牢记。明显的单线:

fruitsArray.filter{ !vegArray.contains($0) }
// ["apple", "orange"]

Is quadratic. No issue if performance isn't important, but it's easily avoidable, using a Set. Now, you could convert both, and use the normal Set methods, but that's unnecessary. Converting to a Set is O(n) (I think, I'm not actually sure), but you're going to have to iterate through the set from fruitsArray anyway, and convert it back to an array in the end. The only advantage of a Set is that is has an O(1) contains method - so you only need to convert the this that you're using contains on. The most efficient way, I think, is:

是二次的。没有问题,如果性能不重要,但使用Set可以轻松避免。现在,你可以转换它们,并使用普通的Set方法,但这是不必要的。转换为Set是O(n)(我想,我不确定),但是你将不得不从fruitsArray迭代遍历集合,并最终将它转换回数组。 Set的唯一优点是它有一个O(1)contains方法 - 所以你只需要转换你正在使用的包含的方法。我认为最有效的方法是:

let vSet = Set(vegArray)
fruitsArray.filter { m in !vSet.contains(m) }

#4


0  

Powerful functional one-liner:

功能强大的单线程:

// Swift 1.2
let differenceResult = fruitsArray.filter({!contains(vegArray, $0)})

// Swift 2.0
let differenceResult = fruitsArray.filter(!vegArray.contains)

and obviously for your other question, simply remove the !:

显然,对于你的其他问题,只需删除!:

// Swift 1.2
let intersectionResult = fruitsArray.filter({contains(vegArray, $0)})

// Swift 2.0
let intersectionResult = fruitsArray.filter(vegArray.contains)