Removing objects from an array based on another array

前端 未结 9 1452
南方客
南方客 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:35

    matt and freytag's solutions are the ONLY ones that account for duplicates and should be receiving more +1s than the other answers.

    Here is an updated version of matt's answer for Swift 3.0:

    var arrayA = ["Mike", "James", "Stacey", "Steve"]
    var arrayB = ["Steve", "Gemma", "James", "Lucy"]
    for word in arrayB {
        if let ix = arrayA.index(of: word) {
            arrayA.remove(at: ix)
        }
    }
    

提交回复
热议问题