swift Array popLast() Function



The popLast() function is used to remove the last element from the given array. It is a pre-defined function, which means we do not need to pass any index value to remove the element, simply we have to call this function and it will automatically remove the last element from the specified array. This function will return an optional value, so to get the actual value we have to forcefully unwarp it using (!). The complexity of this function is O(1).

For example, we have an array = [3, 4, 5, 6, 7]. Now using the popLast() function we removed the last element of the array. Hence the resultant array is [3, 4, 5, 6].

Syntax

Following is the syntax of the popLast() function −

func popLast() -> Self.Element?

Parameters

This function does not take any parameter.

Return Value

This function returns the last element of the specified array. If the array is empty, then it will return nil.

Now we will discuss how to use the popLast() function with the help of the following examples:

Example 1

Swift program to demonstrate the popLast() function. Here we will see how the popLast() function works with different data types.

import Foundation

// Array of string type
var arrayOne = ["Monika", "Romina", "Roy", "Joy"]
print("Original array:", arrayOne)

// Removing last element using popLast() function
let res1 = arrayOne.popLast()!
print("Modified array:\(arrayOne) and removed element: \(res1)")

// Array of integer type
var arrayTwo = [2, 5, 6, 7, 8, 1]
print("\nOriginal array:", arrayTwo)

// Removing last element using popLast() function
let res2 = arrayTwo.popLast()!
print("Modified array:\(arrayTwo) and removed element: \(res2)")

// Array of double type
var arrayThree = [1.2, 25.3, 60.3, 7.2, 83.4, 10.33]
print("\nOriginal array:", arrayThree)

// Removing last element using popLast() function
let res3 = arrayThree.popLast()!
print("Modified array:\(arrayThree) and removed element: \(res3)")

// Array of character type
var arrayFour = ["B", "C", "P", "W", "O"]
print("\nOriginal array:", arrayFour)

// Removing last element using popLast() function
let res4 = arrayFour.popLast()!
print("Modified array:\(arrayFour) and removed element: \(res4)")

Output

Original array: ["Monika", "Romina", "Roy", "Joy"]
Modified array:["Monika", "Romina", "Roy"] and removed element: Joy

Original array: [2, 5, 6, 7, 8, 1]
Modified array:[2, 5, 6, 7, 8] and removed element: 1

Original array: [1.2, 25.3, 60.3, 7.2, 83.4, 10.33]
Modified array:[1.2, 25.3, 60.3, 7.2, 83.4] and removed element: 10.33

Original array: ["B", "C", "P", "W", "O"]
Modified array:["B", "C", "P", "W"] and removed element: O

Example 2

Swift program to demonstrate the use of popLast() function with an empty array.

import Foundation

// Empty array
var array : [Int] = []

// Removeing last element using popLast() function
let removeElement = array.popLast()

if let result = removeElement{
    print("Removed element: \(result)")
}else{
    print("Array is empty")
}

Output

Array is empty

Example 3

Swift program to remove the last element from each row of a two-dimensional array using the popLast() function.

import Foundation

// Two - dimensional array
var array = [
    [3, 2, 3],
    [1, 1, 9],
    [9, 3, 9]
]

// Empty array to store removed elements
var removedElements : [Int] = []

// Print the original array
print("Original array:")
for y in array {
    print(y)
}

// Remove the last element from each row using popLast() function
for x in 0..<array.count {
    let res = array[x].popLast()!
    removedElements.append(res)
}

// Print the modified array
print("Modified array")
for row in array {
    print(row)
}

print("Removed elements: ", removedElements)

Output

Original array:
[3, 2, 3]
[1, 1, 9]
[9, 3, 9]
Modified array
[3, 2]
[1, 1]
[9, 3]
Removed elements:  [3, 9, 9]

Example 4

Swift program to concatenate two arrays and then remove the last element using the popLast() function.

import Foundation

// Arrays of integer type
var arr1 = [3, 5, 7, 8, 9, 10]
var arr2 = [10, 33, 93, 29]

// Concatenating Arrays
var newArray = arr1 + arr2

print("Concatenated Array:", newArray)

// Now removing the last element from the newly created array
let removedElement = newArray.popLast()!
print("Removed element: ", removedElement)

print("After removing the last element the modified array: ", newArray)

Output

Concatenated Array: [3, 5, 7, 8, 9, 10, 10, 33, 93, 29]
Removed element:  29
After removing the last element the modified array:  [3, 5, 7, 8, 9, 10, 10, 33, 93]
Advertisements