remove an element from an array at a specific index in swift

To remove an element at a specific index in a Swift array, you can use the remove(at:) method. Here's an example:

main.swift
var numbers = [1, 2, 3, 4, 5]
let indexToRemove = 2
numbers.remove(at: indexToRemove)
print(numbers) // Output: [1, 2, 4, 5]
125 chars
5 lines

In this example, we first create an array called numbers with the values [1, 2, 3, 4, 5]. Then, we declare a variable called indexToRemove which will contain the index of the element we want to remove.

Finally, we call the remove(at:) method on the numbers array and pass in the indexToRemove variable. This removes the element at the specified index and returns the removed value. Lastly, we print out the updated numbers array which contains all elements except the one at index 2, which has been removed.

gistlibby LogSnag