swift Array last() Function



The last(where:) function of the array in Swift is used to get the last element that satisfies the specified condition from the given array. Here the condition will be represented in the form of closure and the closure will return boolean value. If no element satisfies the given condition then the closure will return nil. So to get the actual value we have to forcefully unwarp the optional value using (!) or if let. The complexity of this function is O(n), where n represents the length of the array.

For example, we have an array = [4, 6, 7, 2, 1, 3, 12]. Now using the last(where: {$0 > 10}) function we will find the last element that matches the given condition that is {$0 > 10}. So the result is 12.

Syntax

Following is the syntax of the last(where:) function −

func first(where condition: (Self.Element) throws -> Bool) rethrows -> Self.Element?

Parameters

Here condition is a closure that takes each element from the given array as an argument and returns a boolean value. If the given condition matches successful, then it will return true. Otherwise, it will return false.

Return Value

This function returns the last element that matches the given condition.

Now we will discuss the use of the last(where:) function with the help of the following examples:

Example 1

Swift program to find the last element from the given array using the last(where:) function.

import Foundation

// Array
let nums = [20, 54, 78, 18, 10, 12]

// Find the last element from the given array
// Using last(where:) function
if let lastElement = nums.last(where: { _ in true }) {
    print("Last Element:", lastElement)
} else {
    print("Array is empty.")
}

Output

Last Element: 12

Example 2

Swift program to find the last element that is greater than 30 with the help of the last(where:) function.

import Foundation

// Array
let nums = [20, 14, 60, 18, 10, 12]

// Find the last element from the given array that is greater than 30
// Using last(where:) function
if let lastElement = nums.last(where: { $0 > 30 }) {
    print("Last Element(greater than 30):", lastElement)
} else {
    print("No element is greater than 30.")
}

Output

Last Element(greater than 30): 60

Example 3

Swift program to find the name and marks of the student who scored less than 150 marks with the help of the last(where:) function.

import Foundation

// Structure
struct Student {
    let name: String
    let marks: Int
}

// Array of Students
let students = [
    Student(name: "Mona", marks: 235),
    Student(name: "Joy", marks: 300),
    Student(name: "Suman", marks: 140),
    Student(name: "Sonali", marks: 340)
]

// Getting the least scored student details
// Using last(where:) function
if let result = students.last(where: { $0.marks < 150 }) {
    print("Student Name: \(result.name) and Marks: \(result.marks)")
} else {
    print("No student found")
}

Output

Student Name: Suman and Marks: 140

Example 4

Swift program to last odd number present in the given array using last(where:) function.

import Foundation

// Input array
let values = [4, 10, 11, 34, 23, 10, 45, 47]

// Find the last odd number in the array using last(where:)
if let lastOddNum = values.last(where: { $0 % 2 != 0 }) {
    print("Last Odd Number:", lastOddNum)
} else {
    print("No odd number is found.")
}

Output

Last Odd Number: 47
Advertisements