Swift Array dropFirst() and dropLast()


In this tutorial, you are going to learn about dropFirst() and dropLast() methods in Swift. One method drop the first element and the other method drops the last element of an array.

Now let’s learn about these methods in detail with examples…

dropFirst() method

The Swift dropFirst() method is used to drop the first element from an array and returns the remaining elements.

Syntax of dropFirst()

Below is given the syntax of the dropFirst() method:

array.dropFirst(Integer_Value)

In the above syntax:

  • array is an array from where we want to drop the element.
  • Integer_Value is the integer type value that represents the first n elements of the array to be removed. Here n is the Integer_Value. It is optional. If we don’t pass any argument, it will drop the first one element.

Example of using dropFirst():

Example 1: drop the first single element:

var colors = ["Green", "Pink", "Yellow", "Red"]

let newColors = colors.dropFirst()

print(newColors)

Output:

["Pink", "Yellow", "Red"]

In the above example, we have used the dropFirst() method to remove the first element from the Swift array. You can see that we have not used any argument in the method.

If we don’t pass any argument to the dropFirst() method, then by default it removes the first element. However, we can still pass a value 1 as an argument to remove the first element which you can see below:

var colors = ["Green", "Pink", "Yellow", "Red"]

let newColors = colors.dropFirst(1)

print(newColors)

Output:

["Pink", "Yellow", "Red"]

Example 2: Drop multiple elements using dropFirst()

We can drop multiple elements by providing the number of elements as the argument to the method. In the example below we are removing the first two elements, so we pass 2 as the argument:

var colors = ["Green", "Pink", "Yellow", "Red"]

let newColors = colors.dropFirst(2)

print(newColors)

Output:

["Yellow", "Red"]

dropLast() method

In Swift, the dropLast() method is used to drop the last element from an array and returns the remaining elements.

Syntax of dropLast()

array.dropLast(Integer_Value)

In the above syntax:

  • array is an array where we want to perform dropping element.
  • Integer_Value is the integer type value that represents the last n elements of the array to be dropped. Here n is an optional integer type value. If we don’t pass any argument, it will drop the last single element.

Example of using dropLast():

Example 1: drop the last single element:

Below is the given example to drop the single last array item:

var colors = ["Green", "Pink", "Yellow", "Red"]

let newColors = colors.dropLast()

print(newColors)

Output:

["Green", "Pink", "Yellow"]

You can notice that we have not passed any argument to the dropLast() method. By default, it will always drop the last single element when we don’t pass any argument.

Below is another example with argument 1 that will give the same result:

var colors = ["Green", "Pink", "Yellow", "Red"]

let newColors = colors.dropLast(1)

print(newColors)

Example 2: Drop multiple elements using dropLast()

Below is the example of a Swift program to drop last multiple elements from an array:

var colors = ["Green", "Pink", "Yellow", "Red"]

let newColors = colors.dropLast(2)

print(newColors)

Output:

["Green", "Pink"]

In the above example, we have passed 2 to the dropLast() method as the argument. If you want to get the last 3 items, then you have to pass 3 as the argument:

colors.dropLast(3)