swift Array max() Function



The max() function of the array is used to find the maximum element from the given array. This function compares all the elements with each other just like we do using the (>) symbol. If the given array is empty, then this function will return nil, which means this function returns an optional value. If we want to get the actual value, then we have to unwarp it with the help of (!) forcefully. The complexity of this function is O(n), where n is the length of the array. This function does not work with a mix-type array.

For example, we have an array = [4, 9, 2, 5, 19, 11]. Now using the max() function we will find the maximum element that is 19.

Syntax

Following is the syntax of the max() function −

func max() -> Self.Element?

Return Value

This function returns the maximum element.

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

Example 1

Swift program to find maximum element from the given arrays using max() function.

import Foundation

// Input Array of string type
let veggies = ["BeetRoot", "Potato", "Tomato", "Peas"]

// Finding maximum element using max() function
let result1 = veggies.max()!

print("Maximum element is", result1)

// Input Array of integer type
let values = [10, 200, 3000, 400, 59990, 55]

// Finding maximum element using max() function
let result2 = values.max()!

print("Maximum element is", result2)

// Input Array of double type
let doubleArray = [34.5, 564.3, 98.4, 9.45, 32.4, 56.4]

// Finding maximum element using max() function
let result3 = doubleArray.max()!

print("Maximum element is", result3)

Output

Maximum element is Tomato
Maximum element is 59990
Maximum element is 564.3

Example 2

Swift program to find the index of the maximum salary in the given array.

import Foundation

// Salary of 7 employees
let salary = [3400, 2300, 4508, 78000, 4500, 1230, 1100]

// Index of the maximum salary
if let index = salary.firstIndex(of: salary.max() ?? 0) {
    print("Index of the maximum Salary: \(index)")
} else {
    print("Array is empty")
}

Output

Index of the maximum Salary: 3

Example 3

Swift program to find maximum element from the given matrix using max() function.

import Foundation

// Input Matrix
let myMatrix = [
    [23, 3, 8, 12], 
    [12, 5, 14, 9], 
    [1, 7, 10, 18],
    [45, 6, 78, 9]
]

// Finding the maximum element from the given array
if let result = myMatrix.flatMap({ $0 }).max() {
    print("Maximum value: \(result)")
} else {
    print("Array is empty")
}

Output

Maximum value: 78

Example 4

Swift program to find the maximum string length in the given array of strings.

import Foundation

// Input array of strings
let strValue = ["apple", "banana", "strawberry", "orange"]

// Finding the maximum length of the element
if let maxLengthElement = strValue.map(\.count).max() {
    print("Maximum string length: \(maxLengthElement)")
} else {
    print("Array is empty")
}

Output

Maximum string length: 10
Advertisements