swift Array first() Function



The first(where:) function of the array in Swift is used to get the first 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. The complexity of this function is O(n), where n represents the length of the array.

For example, we have an array = [4, 9, 2, 5, 19, 11, 0]. Now using the first(where: {$0 < 1}) function we will find the first element that matches the given condition that is {$0 < 1}. So the result is 0.

Syntax

Following is the syntax of the first(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 first element that matches the given condition. 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.

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

Example 1

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

import Foundation

// Array
let nums = [2.4, 5.6, 7.8, 9.7, 12.3]

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

Output

First Element: 2.4

Example 2

Swift program to find the first mark greater than 70 from the given array of the marks using the first(where:) function.

import Foundation

// Marks of all the subjects out of 100
let makrs = [45, 60, 59, 66, 78]

// Find the first highest mark which is greater than 70
// Using first(where:) function
if let firstElement = makrs.first(where: { $0 > 70}) {
    print("First mark greater than 70:", firstElement)
} else {
    print("Not found")
}

Output

First mark greater than 70: 78

Example 3

Swift program to find the name and salary of the employee whose salary is less than 30000 using the last(where:) function.

import Foundation

// Structure
struct Employee {
    let name: String
    let salary: Int
}

// Array of Employees
let emp = [
    Employee(name: "Mohan", salary: 30000),
    Employee(name: "Sonali", salary: 23000),
    Employee(name: "Sumita", salary: 45000),
    Employee(name: "Mohit", salary: 50000)
]

// Getting the first employee whose salary is less than 30000
// Using first(where:) function
if let result = emp.first(where: { $0.salary < 30000}) {
    print("Employee Name: \(result.name) and Salary: \(result.salary)")
} else {
    print("No employee found")
}

Output

Employee Name: Sonali and Salary: 23000

Example 4

Swift program to find the first element from the array whose length is greater than 5 characters using the first(where:) function.

import Foundation

// Input array
let veggies = ["Potato", "Peas", "Corn", "Cabbage", "Tomato"]

// Find the first element with a length greater than 4 characters 
if let result = veggies.first(where: { $0.count > 5 }) {
    print("First word with Length greater than 5 characters:", result)
} else {
    print("No word found with length greater than 5 characters.")
}

Output

First word with Length greater than 5 characters: Potato
Advertisements