iOS swift remove elements of an array from another array

前端 未结 6 852
有刺的猬
有刺的猬 2020-12-13 12:24

I have two arrays

var array1 = new Array [\"a\", \"b\", \"c\", \"d\", \"e\"]
var array2 = new Array [\"a\", \"c\", \"d\"]

I want to remove

6条回答
  •  一生所求
    2020-12-13 12:33

    @Antonio's solution is more performant, but this preserves ordering, if that's important:

    var array1 = ["a", "b", "c", "d", "e"]
    let array2 = ["a", "c", "d"]
    array1 = array1.filter { !array2.contains($0) }
    

提交回复
热议问题