
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
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