Swift Array dropFirst()

In this tutorial, we will learn about the Swift array dropFirst() method with the help of examples.

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


var names = ["Dwight", "Kevin", "Creed"]

// drops first element // and returns remaining elements print(names.dropFirst())
// Output: ["Kevin", "Creed"]

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()

var country = ["Nepal", "Greece", "Spain"]

// drops first element and returns remaining elements print(country.dropFirst())
// original array is not modified print(country)


["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

var languages = ["Swift", "Python", "Java", "C", "C++"]

print("Original Array:", languages)

// remove first two elements from languages print("After dropfirst():", languages.dropFirst(2))


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.

Did you find this article helpful?