24

For example i have an array

var array = [1, 2, 3, 4]

I want to remove item at index 1 then at index 3 "let it be in a for loop".

But removing the item at index 1 will move the item at index 3 to index 2, thus messing up the second removal.

Any suggestions ?

4 Answers 4

41

Given your array

var numbers = [1, 2, 3, 4]

and a Set of indexes you want to remove

let indexesToRemove: Set = [1, 3]

You want to remove the values "2" and "4".

Just write

numbers = numbers
    .enumerated()
    .filter { !indexesToRemove.contains($0.offset) }
    .map { $0.element }

Result

print(numbers) // [1, 3]
Sign up to request clarification or add additional context in comments.

7 Comments

is there any chance you know how to use the same example in Swift 3 ?
Thanks mate I was trying to do it my self and it didn't work i think i was missing out on something thanks again.
An alternative to the chained filter and map is using a single flatMap, e.g. ... .enumerate().flatMap { indexesToRemove.contains($0) ? nil : $1 } (same syntax Swift 2.2/3.0).
Thank for the info mate (Y) @dfri
@appzYourLife I don't know which one I prefer really; filter+map possibly shows intent better, whereas flatMap can be just a little bit neater (imho) in cases where the operation within the map is somewhat trivial :) good to know both alternatives!
|
13

It's simple. delete items from the end.

First delete 3 and after that delete 1

3 Comments

I can't do that when deleting multiple items from a UITableView it is throwing index out of bounds
@Raffi when you change the data source (the array), do you also reloadData on the table? The table needs to know that the row count is now different.
Sort the indices array in descending order first. indices = indices.sorted(by: {$1 < $0})
2

I think the simplest way is removing from behind. So you don't have to worry about the changed index.

var numbers = [1, 2, 3, 4, 5]
let indexesToRemove = [1, 3]
indexesToRemove.sort(by: >)
for i in indexesToRemove {
    numbers.remove(at:i)
}

Comments

1

Swift 3: Use swift closure to perform the same operation.

If your array is like

var numbers = [0, 1, 2, 3, 4, 5]

and indexes you want to remove

let indexesToBeRemoved: Set = [2, 4]

numbers = numbers
    .enumerated()
    .filter { !indexesToRemove.contains($0.offset) }
    .map { $0.element }
and result

print(numbers) // [0, 1, 3, 5]

Swift 3: Here is same operation with JSON Object (dictionary)

var arrayString = [
    [ "char" : "Z" ],
    [ "char" : "Y" ],
    [ "char" : "X" ],
    [ "char" : "W" ],
    [ "char" : "V" ],
    [ "char" : "U" ],
    [ "char" : "T" ],
    [ "char" : "S" ]
]

let arrayIndex = [2, 3, 5]

arrayString = arrayString.enumerated()
    .filter { !arrayIndex.contains($0.0 + 1) }
    .map { $0.1 }

print(arrayString)

[["char": "Z"], ["char": "W"], ["char": "U"], ["name": "T"], ["name": "S"]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.