-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,355
  • 1
  • 41
  • 40
tchristofferson
  • 183
  • 1
  • 13

1 Answers1

3

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,225
  • 7
  • 44
  • 61