Swift Array removeLast()

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


var brands = ["Dell", "Apple", "Razer"]

// removes and returns last element from brands print(brands.removeLast())
// Output: Razer

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

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

// removes and returns last element from country print(country.removeLast())
// print the modified array print(country)


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

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

print("Before removeLast():", languages)

// removes last two elements from languages languages.removeLast(2)
print("After removeLast():", languages)


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

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

Did you find this article helpful?