Removing objects from an array based on another array

前端 未结 9 1476
南方客
南方客 2021-02-05 01:14

I have two arrays like this:

var arrayA = [\"Mike\", \"James\", \"Stacey\", \"Steve\"]
var arrayB = [\"Steve\", \"Gemma\", \"James\", \"Lucy\"]

9条回答
  •  名媛妹妹
    2021-02-05 01:18

    Using the Array → Set → Array method mentioned by Antonio, and with the convenience of an operator, as freytag pointed out, I've been very satisfied using this:

    // Swift 3.x/4.x
    func - (lhs: [Element], rhs: [Element]) -> [Element]
    {
        return Array(Set(lhs).subtracting(Set(rhs)))
    }
    

提交回复
热议问题