
- 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 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'