Swift Array removeFirst()

The removeFirst() method removes the first element from the array.

Example


removeFirst() Syntax

The syntax of the array removeFirst() method is:

array.removeFirst(i: Int)

Here, array is an object of the Array class.


removeFirst() Parameter

The removeFirst() method can take a single parameter:

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

removeFirst() Return Value

  • returns the removed element from the array

Example 1: Swift Array removeFirst()

Output

Nepal
["Greece", "Spain"]

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

After removing the first element, we have printed the modified array.


Example 2: Remove Multiple Number of Elements

Output

Before removeFirst(): ["Swift", "Python", "Java"]
After removeFirst(): ["Java"]

Here, languages.removeFirst(2) removes the first 2 elements from languages.