Swift – Remove Element from Array
In Swift, you can remove an array element by its index, remove the first or last element, remove a range of elements, or remove elements that match a condition.
To remove an element at a specific index, use the remove(at:) method.
array.remove(at: index)
The method modifies the original array and returns the removed element. Swift arrays use zero-based indexing, so the first element is at index 0, the second is at index 1, and so on.
If you describe an element by its human-readable position, subtract one to obtain its array index. For example, the fourth element is stored at index 3.
Remove an Element at a Specific Swift Array Index
In this example, we have an Integer array of size 7 and we shall delete 4th element using remove(at:index) method.
main.swift
var numbers:[Int] = [2, 3, 5, 7, 11, 13, 17]
numbers.remove(at: 3)
numbers.forEach { number in
print("\(number)")
}
Output
2
3
5
11
13
17
The value 7 is the fourth element, so it is removed using index 3. The elements after it shift one position toward the beginning of the array.
Get the Element Returned by remove(at:)
remove(at:) returns the element that was removed. Assign the result to a variable when the deleted value is needed later.
var colors = ["Red", "Green", "Blue"]
let removedColor = colors.remove(at: 1)
print(removedColor)
print(colors)
Output
Green
["Red", "Blue"]
Avoid an Index Out of Range Error
The index passed to remove(at:) must be valid. Passing a negative index or an index equal to or greater than array.count causes a runtime error.
Use the array’s indices collection to check a variable index before removing the element.
var numbers = [10, 20, 30]
let indexToRemove = 2
if numbers.indices.contains(indexToRemove) {
numbers.remove(at: indexToRemove)
}
print(numbers)
Output
[10, 20]
Remove the Last Element from a Swift Array
In this example, we have an Integer array of size 4 and we shall delete the last element using remove(at: array.count-1) method.
main.swift
var months:[String] = ["January", "February", "March", "April"]
months.remove(at: months.count-1)
months.forEach { month in
print("\(month)")
}
Output
January
February
March
Swift also provides removeLast(), which expresses this operation more directly. Do not call it on an empty array.
var months = ["January", "February", "March", "April"]
let removedMonth = months.removeLast()
print(removedMonth)
print(months)
Output
April
["January", "February", "March"]
Remove the First Element from a Swift Array
In this example, we have a String Array and we shall remove the first element using remove(at:0) method.
main.swift
var months:[String] = ["January", "February", "March", "April"]
months.remove(at: 0)
months.forEach { month in
print("\(month)")
}
Output
February
March
April
You can write the same operation using removeFirst(). This method returns the removed element and must not be called when the array is empty.
var months = ["January", "February", "March", "April"]
let removedMonth = months.removeFirst()
print(removedMonth)
print(months)
Output
January
["February", "March", "April"]
Remove a Swift Array Element by Value
Swift arrays remove elements by index rather than directly by value. To remove the first matching value, find its index with firstIndex(of:) and then pass that index to remove(at:).
var fruits = ["Apple", "Banana", "Orange", "Banana"]
if let index = fruits.firstIndex(of: "Banana") {
fruits.remove(at: index)
}
print(fruits)
Output
["Apple", "Orange", "Banana"]
Only the first matching "Banana" is removed. This approach requires the array element type to support equality comparison.
Remove All Matching Elements with removeAll(where:)
Use removeAll(where:) when every element matching a condition should be deleted from the original array.
var numbers = [1, 2, 3, 4, 5, 6]
numbers.removeAll { number in
number.isMultiple(of: 2)
}
print(numbers)
Output
[1, 3, 5]
The closure returns true for elements that should be removed. In this example, all even numbers are deleted.
Filter a Swift Array Without Modifying the Original
Use filter when you want to create a new array containing only the elements that should remain. Unlike removeAll(where:), filter does not mutate the source array.
let numbers = [1, 2, 3, 4, 5, 6]
let oddNumbers = numbers.filter { !$0.isMultiple(of: 2) }
print(numbers)
print(oddNumbers)
Output
[1, 2, 3, 4, 5, 6]
[1, 3, 5]
Remove Multiple Elements from a Swift Array
Use removeSubrange(_:) to remove a continuous range of array elements. The range must be within the array’s valid indices.
var letters = ["A", "B", "C", "D", "E"]
letters.removeSubrange(1...3)
print(letters)
Output
["A", "E"]
Choose the Correct Swift Array Removal Method
| Required operation | Swift method |
|---|---|
| Remove an element at a known index | remove(at:) |
| Remove the first element | removeFirst() |
| Remove the last element | removeLast() |
| Remove the first matching value | firstIndex(of:) with remove(at:) |
| Remove all elements matching a condition | removeAll(where:) |
| Create a new array excluding some elements | filter |
| Remove a continuous range | removeSubrange(_:) |
| Remove every element | removeAll() |
Frequently Asked Questions About Removing Swift Array Elements
How do I remove an array element safely when the index may be invalid?
Check the index with array.indices.contains(index) before calling remove(at:). This prevents an index-out-of-range runtime error.
How do I remove the first occurrence of a value from a Swift array?
Use firstIndex(of:) to locate the value. If it returns an index, pass that index to remove(at:).
How do I remove every occurrence of a value from a Swift array?
Use removeAll { $0 == value } to modify the original array, or use filter { $0 != value } to produce a new array.
What happens to the remaining elements after remove(at:)?
Elements that appear after the removed index shift toward the beginning of the array. Their indices therefore change.
Can removeFirst() or removeLast() be used on an empty array?
No. Calling either method on an empty array causes a runtime error. Check isEmpty first when the array may contain no elements.
Swift Array Removal Summary
Use remove(at:) when the target index is known. Use removeFirst() or removeLast() for an endpoint, firstIndex(of:) for the first matching value, and removeAll(where:) for every element that matches a condition. Validate variable indices before removal to avoid runtime errors.
TutorialKart.com