Swift Array dropFirst()

The dropFirst() method drops the first element and returns the remaining elements in the array.

Example


dropFirst() Syntax

The syntax of the array dropFirst() method is:

array.dropFirst(i: Int)

Here, array is an object of the Array class.


dropFirst() Parameter

The dropFirst() method can take a single parameter:

  • i (optional) - number of elements to be dropped from the beginning of the array

dropFirst() Return Value

  • returns the remaining elements in the array after dropping the first element

Example 1: Swift Array dropFirst()

Output

["Greece", "Spain"]
["Nepal", "Greece", "Spain"]

Here, we have used the dropFirst() method to drop the first element from the country array.

The original array is unchanged because dropFirst() creates a new array instead of modifying the original array.


Example 2: Drop Multiple Number of Elements

Output

Original Array: ["Swift", "Python", "Java", "C", "C++"]
After dropfirst(): ["Java", "C", "C++"]

Here, languages.dropFirst(2) removes the first 2 elements from languages and returns the remaining elements.