Swift Arrays - Removing Elements

In this article, we'll go over different ways to remove elements from an Array in Swift.

There are several methods you can use to remove elements from a Swift Array.

Let's look at each method with examples and actual code execution results.

Removing by Index with remove()

If you know the index of the element you want to remove from a Swift Array, you can use the remove() method.

The syntax is array.remove(at: index), where you specify the index.

The remove() method also returns the removed element, so you can store it in a variable if you need the value that was removed.


Let's remove the second element from the Array using remove().

In Swift, array indexes start at 0, so the index for the second element is 1.

var names = ["Smith", "Williams", "Miller", "Brown"]
print(names)

let removed = names.remove(at: 1)
print(names)
print(removed)

The output looks like this. The second element, "Williams," is removed from the names array and is stored in the removed variable.

["Smith", "Williams", "Miller", "Brown"]
["Smith", "Miller", "Brown"]
Williams

Removing a Range with removeSubrange()

Using Swift's range operators, you can specify a range of indexes with the removeSubrange() method to remove multiple elements.

For example, to remove the 2nd and 3rd elements (indexes 1 through 2) from the Array, you can do the following:

var names = ["Smith", "Williams", "Miller", "Brown"]
print(names)

names.removeSubrange(1...2)
print(names)

The output looks like this. The second and third elements have been removed.

["Smith", "Williams", "Miller", "Brown"]
["Smith", "Brown"]

Removing the First Element with removeFirst()

To remove the first element of an Array in Swift, you can use the removeFirst() method.

Without arguments, removeFirst() deletes the very first element and returns it as the result.

If you pass a number as an argument, like removeFirst(n), it removes that many elements from the beginning of the array. In this case, it does not return values.


Here's an example using removeFirst():

var names = ["Smith", "Williams", "Miller", "Brown"]
print(names)

let removed = names.removeFirst()
print(names)
print(removed)

The output shows that the first element, "Smith," is removed from names, and the value is stored in removed.

["Smith", "Williams", "Miller", "Brown"]
["Williams", "Miller", "Brown"]
Smith

If you want to remove the first two elements, you can do this:

var names = ["Smith", "Williams", "Miller", "Brown"]
print(names)

names.removeFirst(2)
print(names)

The output shows that the first two elements of the array are removed.

["Smith", "Williams", "Miller", "Brown"]
["Miller", "Brown"]

Removing the Last Element with removeLast()

To remove the last element of a Swift Array, you can use the removeLast() method.

It works almost the same way as removeFirst(), except it operates from the end of the array. Without arguments, it deletes the very last element and returns it.

If you specify a number, like removeLast(n), it removes that many elements from the end of the array. In this case, it does not return values.


Here's an example using removeLast():

var names = ["Smith", "Williams", "Miller", "Brown"]
print(names)

let removed = names.removeLast()
print(names)
print(removed)

The output shows that the last element, "Brown," is removed from names, and the value is stored in removed.

["Smith", "Williams", "Miller", "Brown"]
["Smith", "Williams", "Miller"]
Brown

If you want to remove the last two elements, you can do this:

var names = ["Smith", "Williams", "Miller", "Brown"]
print(names)

names.removeLast(2)
print(names)

The output shows that the last two elements of the array are removed.

["Smith", "Williams", "Miller", "Brown"]
["Smith", "Williams"]

Removing All Elements with removeAll()

To remove all elements from a Swift Array, you can use the removeAll() method.

Here's an example of clearing all elements from an array with removeAll():

var names = ["Smith", "Williams", "Miller", "Brown"]
print(names)

names.removeAll()
print(names)

The output shows that all the elements have been removed.

["Smith", "Williams", "Miller", "Brown"]
[]

Removing Elements by Value with removeAll(where:)

With the removeAll(where:) method, you can specify a condition to remove all matching elements from an array.

For example, if you want to remove all occurrences of "Williams" from ["Smith", "Williams", "Miller", "Brown", "Williams"], you can do this:

var names = ["Smith", "Williams", "Miller", "Brown", "Williams"]
print(names)

names.removeAll(where: {$0 == "Williams"})
print(names)

The output shows that all occurrences of "Williams" are removed.

["Smith", "Williams", "Miller", "Brown", "Williams"]
["Smith", "Miller", "Brown"]

Removing elements from a Swift Array 1


That's how you can remove elements from an Array in Swift using different methods.