[英]How to compare two arrays? And get the element which are not in the other one?
假設我們有兩個數組:
var a = ["Tom", "Harry", "jim", "Allen"]
var b = ["Tom", "Harry", "jim", "Allen", "Chloe", "Jasmine"]
現在我想要一個數組,其中包含以下元素:
var requiredArray = ["Chloe","Jasmine"]
您的規范不夠具體,但這是一種簡單的方法:
var a = ["Tom", "Harry", "jim", "Allen"]
var b = ["Tom", "Harry", "jim", "Allen", "Chloe", "Jasmine"]
let c = Array(Set(b).subtract(a))
另一種方法是使用filter
(請參閱此處 ):
let result = b.filter { !a.contains($0) }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.