Remove multiple indices from array

后端 未结 8 2108
一整个雨季
一整个雨季 2020-12-30 12:31

I have an array and I want to remove a bunch of indices

var arr = [0,1,2,3,4,5,6]
var rmIndices = [1,4,5]

What is the best way to remove in

8条回答
  •  伪装坚强ぢ
    2020-12-30 13:20

    For Swift 3

        var arr = [0,1,2,3,4,5,6]
        let rmIndices = [1,4,5]
        arr = arr.filter{ !rmIndices.contains($0) }
        print(arr)
    

    if you want to produce output very fastly then you can use

        var arr = [0,1,2,3,4,5,6]
        let rmIndices = [1,4,5]
        arr = Array(Set(arr).subtracting(rmIndices))
        print(array)
    

    But it will change order of your array

提交回复
热议问题