swift Array min() Function



The min() function of the array is used to find the minimum element from the specified array. 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 forcefully unwarp it with the help of (!). The complexity of this function is O(n), where n is the length of the array.

For example, we have an array = [12, 33, 56, 1, 66, 3]. Now using the min() function we will find the minimum element that is 1.

Syntax

Following is the syntax of the min() function −

func min() -> Self.Element? 

Return Value

This function returns the minimum element.

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

Example 1

Swift program to find the minimum element from the given arrays of different data types using the min() function.

import Foundation

// Array of integer type
let arrayOne : [Int] = [3, 45, 12, 199, 18, 0, 1]

// Finding minimum element from the given array using min() function
let resultOne = arrayOne.min()!
print("Minimum element of Array:\(arrayOne) is \(resultOne)")

// Array of string type
let arrayTwo : [String] = ["Hello", "Hi", "H"]

// Finding minimum element from the given array using min() function
let resultTwo = arrayTwo.min()!
print("Minimum element of Array:\(arrayTwo) is \(resultTwo)")

// Array of double type
let arrayThree : [Double] = [23.5, 45.3, 90.3, 11.2, 32.2]

// Finding the minimum element from the given array using the min() function
let resultThree = arrayThree.min()!
print("Minimum element of Array:\(arrayThree) is \(resultThree)")

Output

Minimum element of Array:[3, 45, 12, 199, 18, 0, 1] is 0
Minimum element of Array:["Hello", "Hi", "H"] is H
Minimum element of Array:[23.5, 45.3, 90.3, 11.2, 32.2] is 11.2

Example 2

Swift program to find minimum number from the given 2-D array using min() function.

import Foundation

// 2-D array
let twoDArray = [
[2, 14, 6, 7], 
[1, 5, -1, 23], 
[27, 89, 11, 13],
[3, 19, 3, 2]
]

// finding the minimum number from the given 2-D array using the min() function
if let miniNum = twoDArray.flatMap({ $0 }).min() {
    print("Minimum Number: \(miniNum)")
} else {
    print("Try again! the given array is empty")
}

Output

Minimum Number: -1

Example 3

Swift program to find the age difference between the youngest and oldest members in the group. Here we first find the maximum and minimum age using the max() and min() functions and then calculate their difference.

import Foundation

// Age of all the members of group A
let ageGroupA = [18, 23, 34, 26, 38, 21]

// Minimum Age
let minAge = ageGroupA.min() ?? 0

// Maximum Age
let maxAge = ageGroupA.max() ?? 0

// Age difference
let difference = maxAge - minAge

print("Age difference between the youngest and oldest member is:\(difference) years")

Output

Age difference between the youngest and oldest member is:20 years

Example 4

Swift program to find the minimum string from the given array and then find the reverse of that minimum string. Here the min() function finds the minimum string according to the first character of the element not by the length of the string.

import Foundation

// Array of string type
let programmingLanguages = ["Swift", "Python", "Java", "CSharp"]

// Check if the array is not empty before using min()
if let miniStr = programmingLanguages.min() {

    // Find the reverse of the minimum string
    let reverseStr = String(miniStr.reversed())

    print("The minimum string is: '\(miniStr)' and the reverse of the minimum string is: '\(reverseStr)'")
} else {
    print("Array is empty.")
}

Output

The minimum string is: 'CSharp' and the reverse of the minimum string is: 'prahSC'
Advertisements