-1

Does anyone know how to remove an element from an array without knowing exactly what spot it is in?

var array = ["A", "B", "C"]

how would I remove the "A" if there were only a few in the whole array and it contained thousands of strings(Just remove one "A" not all of them)?

0

1 Answer 1

4

Just like this:

var array = ["A", "B", "C"]

if let firstIndex = array.indexOf("A") { // Get the first index of "A"
    array.removeAtIndex(firstIndex) // Remove element at that index
}

Swift 3:

if let firstIndex = array.index(of: "A") {
    array.remove(at: firstIndex)
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.