Swift 3 Array,使用.remove(at:i)一次删除多个项目

Con*_*sed 17 arrays xcode ios swift indexpath

是否可以从数组中删除多个项目,同时使用索引位置按照.remove(at:i)类似:

伪代码:

myArray.remove(at: 3, 5, 8, 12)
Run Code Online (Sandbox Code Playgroud)

如果是这样,这样做的语法是什么?


更新:

我正在尝试这个,它有效,但下面答案中的扩展更具可读性和合理性,并且实现了与伪代码完全相同的目标.

创建一系列"位置":[3,5,8,12]

let sorted = positions.sorted(by: { $1 < $0 })
for index in sorted
{
    myArray.remove(at: index)
}
Run Code Online (Sandbox Code Playgroud)

Tha*_*ham 24

如果索引是连续使用removeSubrange方法,则可能.例如,如果要删除索引3到5处的项目:

myArray.removeSubrange(ClosedRange(uncheckedBounds: (lower: 3, upper: 5)))
Run Code Online (Sandbox Code Playgroud)

对于非连续索引,我建议删除索引较大的项目到较小的索引.除了代码可能更短之外,我无法想到在同一行中"同时"删除项目.您可以使用扩展方法执行此操作:

extension Array {
  mutating func remove(at indexes: [Int]) {
    for index in indexes.sorted(by: >) {
      remove(at: index)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后:

myArray.remove(at: [3, 5, 8, 12])
Run Code Online (Sandbox Code Playgroud)

更新:使用上面的解决方案,您需要确保索引数组不包含重复索引.或者您可以避免重复,如下所示:

extension Array {
    mutating func remove(at indexes: [Int]) {
        var lastIndex: Int? = nil
        for index in indexes.sorted(by: >) {
            guard lastIndex != index else {
                continue
            }
            remove(at: index)
            lastIndex = index
        }
    }
}


var myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
myArray.remove(at: [5, 3, 5, 12]) // duplicated index 5
// result: [0, 1, 2, 4, 6, 7, 8, 9, 10, 11, 13] only 3 elements are removed
Run Code Online (Sandbox Code Playgroud)


Kru*_*nal 18

使用数组元素的索引删除元素:

  1. 字符串和索引的数组

    let animals = ["cats", "dogs", "chimps", "moose", "squarrel", "cow"]
    let indexAnimals = [0, 3, 4]
    let arrayRemainingAnimals = animals
        .enumerated()
        .filter { !indexAnimals.contains($0.offset) }
        .map { $0.element }
    
    print(arrayRemainingAnimals)
    
    //result - ["dogs", "chimps", "cow"]
    
    Run Code Online (Sandbox Code Playgroud)
  2. 整数和索引的数组

    var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    let indexesToRemove = [3, 5, 8, 12]
    
    numbers = numbers
        .enumerated()
        .filter { !indexesToRemove.contains($0.offset) }
        .map { $0.element }
    
    print(numbers)
    
    //result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
    
    Run Code Online (Sandbox Code Playgroud)



使用另一个数组的元素值删除元素

  1. 整数数组

    var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    let elementsTobeRemoved = [3, 5, 8, 12]
    let arrayResult = numbers.filter { element in
        return !elementsTobeRemoved.contains(element)
    }
    print(arrayResult)
    
    //result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
    
    Run Code Online (Sandbox Code Playgroud)
  2. 字符串数组

    let arrayLetters = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
    let arrayRemoveLetters = ["a", "e", "g", "h"]
    let arrayRemainingLetters = arrayLetters.filter {
        !arrayRemoveLetters.contains($0)
    }
    
    print(arrayRemainingLetters)
    
    //result - ["b", "c", "d", "f", "i"]
    
    Run Code Online (Sandbox Code Playgroud)


lin*_*uoc 6

斯威夫特4

extension Array {

    mutating func remove(at indexs: [Int]) {
        guard !isEmpty else { return }
        let newIndexs = Set(indexs).sorted(by: >)
        newIndexs.forEach {
            guard $0 < count, $0 >= 0 else { return }
            remove(at: $0)  
        }
    }

}

var arr = ["a", "b", "c", "d", "e", "f"]

arr.remove(at: [2, 3, 1, 4])

result: ["a", "f"]
Run Code Online (Sandbox Code Playgroud)


Kam*_*icz 5

简单明了的解决方案,只需Array扩展即可:

extension Array {

    mutating func remove(at indices: [Int]) {
        Set(indices)
            .sorted(by: >)
            .forEach { rmIndex in
                self.remove(at: rmIndex)
            }
    }
}
Run Code Online (Sandbox Code Playgroud)
  • Set(indices) -确保唯一性
  • .sorted(by: >) -函数从最后到第一删除元素,因此在删除过程中,我们确保索引正确