Remove multiple indices from array

后端 未结 8 2170
一整个雨季
一整个雨季 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:19

    Rather than a list of indices to remove, it may be easier to have a list of indices to keep, which you can do using the Set type:

    let rmIndices = [1,4,5]
    let keepIndices = Set(arr.indices).subtract([1,4,5])
    

    Then you can use PermutationGenerator to create a fresh array of just those indices:

    arr = Array(PermutationGenerator(elements: arr, indices: keepIndices))
    

提交回复
热议问题