-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)?

dan
  • 9,695
  • 1
  • 42
  • 40
tchristofferson
  • 183
  • 1
  • 1
  • 14

1 Answers1

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)
}
Kametrixom
  • 14,673
  • 7
  • 45
  • 62