Swift – Remove Element from Array at Specific Index
To remove element from Array at specific index in Swift, call remove(at:)
method on this array and pass the index as value for at
parameter.
remove(at:)
method removes the element at given index.
The syntax to call remove(at:)
method on the array with specific index
is
arrayName.remove(at:index)
The index
at which we would like to remove the element from array has to be within the index limits of this array.
Examples
In the following program, we will take an array fruits
, and remove the element from this array at index 2
.
main.swift
var fruits = ["apple", "banana", "cherry", "mango", "guava"]
var index = 2
fruits.remove(at:index)
print(fruits)
Output

The resulting array fruits
has the element "cherry"
removed at specified index 2
.
Let us try to remove an element at index 8
, when the array length is only 5
. We get runtime error as shown in the following program.
main.swift
var fruits = ["apple", "banana", "cherry", "mango", "guava"]
var index = 8
fruits.remove(at:index)
print(fruits)
Output

Conclusion
In this Swift Tutorial, we learned how to remove an element from array, at specified index.