Swift Array removeLast()

The removeLast() method removes the last element from the array.

Example


removeLast() Syntax

The syntax of the array removeLast() method is:

array.removelast(i: Int)

Here, array is an object of the Array class.


removeLast() Parameter

The removeLast() method can take a single parameter:

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

removeLast() Return Value

  • returns the removed element from the array

Example 1: Swift Array removeLast()

Output

Spain
["Nepal", "Greece"]

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

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


Example 2: Remove Multiple Number of Elements

Output

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

Here, languages.removeLast(2) removes the last 2 elements from languages.