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

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

伪代码:

myArray.remove(at: 3, 5, 8, 12) 

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


更新

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

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

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

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

 myArray.removeSubrange(ClosedRange(uncheckedBounds: (lower: 3, upper: 5))) 

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

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

然后:

 myArray.remove(at: [3, 5, 8, 12]) 

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

 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 

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

  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"] 
  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] 

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

  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] 
  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"] 

简单明了的解决方案,只是Array扩展:

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

斯威夫特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"] 

您可以创建一组要删除的索引。

 var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let indexSet = [3, 5, 8, 12] indexSet.reversed().forEach{ array.remove(at: $0) } print(array) 

输出:[0,1,2,4,6,7,9,10,11]

如果索引是连续的,那么使用removeSubrange

 array.removeSubrange(1...3) /// Will remove the elements from 1, 2 and 3 positions. 

根据NSMutableArray API,我建议将索引实现为IndexSet

你只需要反转顺序。

 extension Array { mutating func remove(at indexes: IndexSet) { indexes.reversed().forEach{ self.remove(at: $0) } } } 

请参阅此答案,提供更有效的算法。