Swift 3 Array, delete multiple items at once with .remove (at: i)

Is it possible to remove more than one element from the array, at the same time, using the location of the indices according to .remove (at: i) like:

Pseudocode:

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

And if so, what is the syntax for this?


UPDATE

I tried to do this, it worked, but the extension in the answer below is much readable and reasonable and achieves a goal exactly the same as the pseudocode.

an array of "positions" is created: [3, 5, 8, 12]

 let sorted = positions.sorted(by: { $1 < $0 }) for index in sorted { myArray.remove(at: index) } 
+5
source share
3 answers

Perhaps if the indexes are continuous using the removeSubrange method. For example, if you want to remove items with an index of 3 - 5:

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

For non-continuous indexes, I would recommend removing items with a larger index to a smaller one. There is no benefit that I could think of deleting items "at the same time" in the same layer, except that the code may be shorter. You can do this using the extension method:

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

Then:

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

UPDATE: using the solution above, you need to make sure that the index array does not contain duplicate indexes. Or you can avoid duplicates as shown below:

 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 
+12
source

Delete elements using array element indices:

  • Array of strings and indexes

     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"] 
  • Array of integers and indices

     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] 



Delete elements using element value of another array

  1. Arrays of Integers

     let arrayResult = numbers.filter { element in return !indexesToRemove.contains(element) } print(arrayResult) //result - [0, 1, 2, 4, 6, 7, 9, 10, 11] 
  2. Line arrays

     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"] 
+10
source

According to the NSMutableArray API NSMutableArray I recommend implementing indexes as an IndexSet .

You just need to invert the order.

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

Source: https://habr.com/ru/post/1258015/


All Articles