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