Instance Method

remove(at:)

Removes and returns the element at the specified position.

Declaration

@discardableResult mutating func remove(at index: Int) -> Array.Element

Parameters

index

The position of the element to remove. index must be a valid index of the array.

Return Value

The element at the specified index.

Discussion

All the elements following the specified position are moved up to close the gap.

var measurements: [Double] = [1.1, 1.5, 2.9, 1.2, 1.5, 1.3, 1.2]
let removed = measurements.remove(at: 2)
print(measurements)
// Prints "[1.1, 1.5, 1.2, 1.5, 1.3, 1.2]"

Complexity: O(n), where n is the length of the array.

Relationships

See Also

Removing Elements

func remove(at: Int)

Removes and returns the element at the specified position.

Beta
func removeFirst()

Removes and returns the first element of the collection.

Beta
func removeFirst(Int)

Removes the specified number of elements from the beginning of the collection.

Beta
func removeLast()

Removes and returns the last element of the collection.

Beta
func removeLast(Int)

Removes the specified number of elements from the end of the collection.

Beta
func removeSubrange(Range<Int>)

Removes the elements in the specified subrange from the collection.

Beta
func removeSubrange<R>(R)

Removes the elements in the specified subrange from the collection.

Beta
func removeAll(keepingCapacity: Bool)

Removes all elements from the collection.

func removeAll(keepingCapacity: Bool)

Removes all elements from the array.

Beta
func popLast()

Removes and returns the last element of the array.