Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 Array, remove more than one item at once, with .remove(at: i)

Tags:

Is it possible to remove more than one item from an array, at the same time, using index locations as per .remove(at: i) kind of like:

Pseudo code:

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

And if so, what's the syntax for doing this?


UPDATE:

I was trying this, it worked, but the extension in the answer below is much more readable, and sensible, and achieves the goal of one that's exactly as the pseudo code.

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) } 
like image 492
Confused Avatar asked Oct 11 '16 10:10

Confused


People also ask

How do I remove multiple elements from an array?

The idea is to use JavaScript filter() method to remove multiple items from an array. The following code example returns the new array of filtered values using the latest includes() method. Here's an alternative version which uses the indexOf() method instead of the includes() method.

How do I remove an item from an array by value?

To remove an object from an array by its value:Call the findIndex() method to get the index of the object in the array. Use the splice() method to remove the element at that index. The splice method changes the contents of the array by removing or replacing existing elements.

How do you clear an array in Swift?

To create an empty string array in Swift, specify the element type String for the array and assign an empty array to the String array variable.


2 Answers

It's possible if the indexes are continuous using removeSubrange method. For example, if you would like to remove items at index 3 to 5:

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

For non-continuous indexes, I would recommend remove items with larger index to smaller one. There is no benefit I could think of of removing items "at the same time" in one-liner except the code could be shorter. You can do so with an 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 would need to ensure the indexes array does not contain duplicated indexes. Or you can avoid the duplicates as 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 
like image 164
Thanh Pham Avatar answered Sep 17 '22 14:09

Thanh Pham


Remove elements using indexes of an array elements:

  1. 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"] 
  2. Array of Integers and indexes

    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] 



Remove elements using element value of another array

  1. Arrays of integers

    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. Arrays of strings

    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"] 
like image 38
Krunal Avatar answered Sep 19 '22 14:09

Krunal