Open In App

How to Remove the last element from an Array in Swift?

Last Updated : 02 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Just like other programming languages Swift also support array. An array is an ordered generic collection that is used to store the same type of values like int, string, float, etc., you are not allowed to store the value of another type in an array, for example, you have an int type array in this array you are not allowed to store float values, if you try to do so you will get an error. It also stores duplicate values of the same type. When an array is assigned to a variable then that array is mutable which means you are allowed to change the content and the size of the array. And when an array is assigned to a constant then that array is immutable which means you are not allowed to change the content and size of the array. In the Swift array, we are allowed to remove the last element from the given array. To do this task we use the removeLast() function. This function deletes or removes the last element present in the array. We can also remove multiple ending elements from the array using the removeLast() function.

Syntax:

arrayName.removeLast(x: Int)

Here, 

arrayName is the object of the array class.

Parameter: This function takes only one parameter. It is an optional parameter and it is used to remove the number of elements from the ending of the array

Return Value: This function returns the removed element.

Example:

Swift




// Swift program to remove last element from 
// the ending of the array
import Swift
  
// Creating an array of Emp Name
// Here the array is of string type
var GfgEmpName = ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"]
  
print("Geeks's employee name before removing:", GfgEmpName)
  
// Removing the last element from the GfgEmpName array
// Using removeLast() function
GfgEmpName.removeLast()
  
// Displaying the final result
print("Geeks's employee name after removing: ", GfgEmpName)
  
// Creating an array of salary
// Here the array is of int type
var GfgEmpSalary = [23000, 1000, 90000, 30000, 56000, 21000, 6000]
print(".................................")
  
print("EmpSalary before removing elements:", GfgEmpSalary)
  
// Removing the last elements from the GfgEmpSalary array
// Using removeFirst() function
GfgEmpSalary.removeLast()
  
// Displaying the final result
print("EmpSalary after removing elements:", GfgEmpSalary)


Output:

Geeks's employee name before removing: ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"]
Geeks's employee name after removing:  ["Sumit", "Poonam", "Punit", "Bittu"]
.................................
EmpSalary before removing elements: [23000, 1000, 90000, 30000, 56000, 21000, 6000]
EmpSalary after removing elements: [23000, 1000, 90000, 30000, 56000, 21000]

Removing multiple elements from the array

In Swift, using the removeLast() function we can also remove multiple elements from the ending of the array. To do this we only need to pass the number which represents the total number of elements to remove from the array in the function. For example, if we want to remove starting 2 elements from the array then we need to pass 2 in this function. 

Syntax:

arrayName.removeLast(2)

Example:

Swift




// Swift program to remove multiple elements from 
// the ending of the array
import Swift
  
// Creating an array of Emp Name
// Here the array is of string type
var GfgEmpName = ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"]
  
print("Geeks's employee name before removing:", GfgEmpName)
  
// Removing the last two elements from the GfgEmpName array
// Using removeLast() function
GfgEmpName.removeLast(2)
  
// Displaying the final result
print("Geeks's employee name after removing: ", GfgEmpName)
  
// Creating an array of salary
// Here the array is of int type
var GfgEmpSalary = [23000, 1000, 90000, 30000, 56000, 21000, 6000]
print(".................................")
  
print("EmpSalary before removing elements:", GfgEmpSalary)
  
// Removing the last four elements from the GfgEmpSalary array
// Using removeFirst() function
GfgEmpSalary.removeLast(4)
  
// Displaying the final result
print("EmpSalary after removing elements:", GfgEmpSalary)


Output:

Geeks's employee name before removing: ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"]
Geeks's employee name after removing:  ["Sumit", "Poonam", "Punit"]
.................................
EmpSalary before removing elements: [23000, 1000, 90000, 30000, 56000, 21000, 6000]
EmpSalary after removing elements: [23000, 1000, 90000]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads