← All languages
Swift function of the day
Random

Array.dropFirst

Return a subsequence with the specified number of initial elements removed.

Description

The dropFirst method returns a subsequence containing all but the first k elements. If k is greater than the collection's count, the result is empty. The default value of k is 1.

The returned ArraySlice shares storage with the original array. dropFirst is useful for skipping headers or processing all elements after the first.

dropFirst has O(1) complexity for random-access collections.

Arguments

NameDescriptionOptional
k The number of elements to drop from the start. Defaults to 1. Yes

Example

let numbers = [1, 2, 3, 4, 5]
let rest = numbers.dropFirst(2)
print(Array(rest))  // [3, 4, 5]
let tail = numbers.dropFirst()
print(Array(tail))  // [2, 3, 4, 5]

Reference