swift Array last Property



An array is a collection that is used to store and manage values of the same types in an ordered list. The elements that are stored in the array are indexed means the first element is present at index zero. So Swift provides a pre-defined property to named last property. The last property is used to find the last element of the array. It is a quick response property and returns the last element without iterating through each element of the array.

For example, we have an array of integer types: Array = [2, 4, 5, 7, 8]. Now using the last property we will find the last element of the array. Hence the last element of the array is 8.

Syntax

Following is the syntax of the last property −

var last: Self.Element? {get}

Return Value

This property returns the last element of the array. This property returns an optional value so to get the actual value we have to forcefully unwrap it using (!) or if let.

Now we will discuss the usage of the last property with the help of the following examples:

Example 1

Swift program to find the last element of the given arrays using the last property.

import Foundation

// Input array of string type
let input1 = ["Xop", "Rat", "WWE"]

// Finding the last element using the last property
let result1 = input1.last!
print("Last element of the Array:", result1)

// Input array of int type
let input2 = [4, 6, 2, 9, 12, 8]

// Finding the last element using the last property
let result2 = input2.last!
print("Last element of the Array:", result2)

// Input array of double type
let input3 = [3.5, 99.33, 91.2, 9.2]

// Finding the last element using the last property
let result3 = input3.last!
print("Last element of the Array:", result3)

Output

Last element of the Array: WWE
Last element of the Array: 8
Last element of the Array: 9.2

Example 2

Swift program to find the last element from the given matrix.

import Foundation

// Input Matrix
let inputMatrix: [[Int]] = [
    [3, 2, 1],
    [4, 0, 8],
    [1, 8, 4]
]

// Function to find the last element from the matrix
func matrixLastElement(in inputMatrix: [[Int]]) -> Int? {

    // If matrix is empty
    guard let lastRow = inputMatrix.last else {
        return nil 
    }
    // If last row is empty  
    guard let lastItem = lastRow.last else {
        return nil 
    }
    
    return lastItem
}

// Find the last element from the matrix
if let result = matrixLastElement(in: inputMatrix) {
    print("Last element of the given matrix is: \(result)")
} else {
    print("Not Found")
}

Output

Last element of the given matrix is: 4

Example 3

Swift program to find the sum of the last two elements of the given matrix.

import Foundation

// Function to find the sum of the last two elements in the matrix
func sum(in matrixArr: [[Int]]) -> Int? {

    // Return nil if the given matrix is empty or the last row doesn't have enough elements
    guard let lastRow = matrixArr.last, lastRow.count >= 2 else {
        return nil 
    }
    
    // Finding the last two elements
    let lastItem = lastRow[lastRow.count - 1]
    let secondLastItem = lastRow[lastRow.count - 2]
    
    // Calculating the sum of the last two elements
    let summ = lastItem + secondLastItem 
    return summ
}


// Input matrix
let matrixArr: [[Int]] = [
    [10, 2, 3],
    [4, 15, 6],
    [1, 3, 19]
]

if let result = sum(in: matrixArr) {
    print("Sum: \(result)")
} else {
    print("Not found")
}

Output

Sum: 22

Example 4

Swift program to concatenate the last elements of the given two arrays. Here first we find the last elements of the two arrays using the last property and then concatenate them using the + operator.

import Foundation

// Arrays of string type
let arrayOne = ["Monika", "Romina", "Roy", "Joy"]
let arrayTwo = ["Delhi", "Mumbai", "Pune"]

// Finding the last elements
let resultOne = arrayOne.last!
let resultTwo = arrayTwo.last!

// Concatenating last elements
let finalResult = resultOne + resultTwo

print("Concatenated String:", finalResult)

Output

Concatenated String: JoyPune
Advertisements