swift Array dropLast() Function



The dropLast() function of an array is used to remove the specified number of elements from the end of the given array. With the help of this function, we can remove as many as elements we want from the end of the array. If the number of elements we want to remove is greater than the size of the array, then this function will return an empty array. The complexity of this function is O(1).

For example, we have an array = [30, 20, 11, 19, 17, 3]. Now using the dropLast(3) function we removed the last 3 elements from the array. Hence the resultant array is [30, 20, 11].

Syntax

Following is the syntax of the dropLast() function −

func dropLast(_x: Int) -> Self.SubSequence

Parameters

This function takes only one parameter which is x. Here x represents the number of elements we want to remove from the end of the given array. If this function does not contain any parameter, then it will remove the last element from the given array.

Return value

This function returns an array after removing the last elements.

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

Example 1

Swift program to demonstrate the dropLast() function. Here we will see how to drop the last element of the given arrays of different data types.

import Foundation

// Array of integer type
var numberOne = [3, 59, 10, 33, 10]
print("Original array:", numberOne)

// Removing last two elements using dropLast() function
print("Updated array:\(numberOne.dropLast(2))")

// Array of string type
var strings = ["Cold", "Hot", "Mild", "Small"]
print("\nOriginal array:", strings)

// Removing last three elements using dropLast() function
print("Updated array:\(strings.dropLast(3))")

// Array of double type
var doubleNums = [2.4, 23.4, 10.34, 11.3, 12.3, 11.0]
print("\nOriginal array:", doubleNums)

// Removing last four elements using dropLast() function
print("Updated array:\(doubleNums.dropLast(4))")

// Array of character type
var chars = ["a", "b", "c", "d"]
print("\nOriginal array:", chars)

// Removing last one element using dropLast() function
print("Updated array:\(chars.dropLast(1))")

Output

Original array: [3, 59, 10, 33, 10]
Updated array:[3, 59, 10]

Original array: ["Cold", "Hot", "Mild", "Small"]
Updated array:["Cold"]

Original array: [2.4, 23.4, 10.34, 11.3, 12.3, 11.0]
Updated array:[2.4, 23.4]

Original array: ["a", "b", "c", "d"]
Updated array:["a", "b", "c"]

Example 2

Swift program to demonstrate dropLast() function without any parameter. Here we do not pass any parameter in the dropLast() function as a result it removes only the last element from the specified array.

import Foundation

// Array of integer type
var numbers = [4, 6, 77, 12, 10, 19, 12]
print("Original array:", numbers)

// Using dropLast() function without any parameter
print("Modified Array:", numbers.dropLast())

Output

Original array: [4, 6, 77, 12, 10, 19, 12]
Modified Array: [4, 6, 77, 12, 10, 19]

Example 3

Swift program to demonstrate dropLast() function. Here we pass parameters that is greater than the size of the given array as a result this function return empty array.

import Foundation

// Array of integer type
var numbers = [3, 40, 0, 11, 10, 19, 23]
print("Original array:", numbers)

// Using dropLast() function 
print("Modified Array:", numbers.dropLast(12))

Output

Original array: [3, 40, 0, 11, 10, 19, 23]
Modified Array: []

Example 4

Swift program to remove the last two elements from each row in the given matrix using the dropLast() function.

import Foundation

// Function to remove last two elements from each row 
func removeLastElements(inputMatrix: [[Int]]) -> [[Int]] {

    // Creating a new matrix 
    let resultantMatrix = inputMatrix.map { x in
    
        // Dropping the last two elements from each row
        return Array(x.dropLast(2))
    }
    
    return resultantMatrix
}

let matrix = [
[3, 5, 6, 7, 8, 9],
[4, 2, 3, 1, 1, 1],
[2, 3, 5, 6, 6, 7],
[3, 2, 1, 2, 1, 1]
]

print("New Matrix(after removing last two elements):", removeLastElements(inputMatrix: matrix))

Output

New Matrix(after removing last two elements): [[3, 5, 6, 7], [4, 2, 3, 1], [2, 3, 5, 6], [3, 2, 1, 2]]
Advertisements