The min()
method can be used to get the minimum element of an array.
arr.min()
arr: This is the array whose minimum element we want to find.
The value returned is the minimum element of the array. If the array has no element, a nil
value is returned.
// create arrays let evenNumbers = [2, 10, 4, 12, 6, 8] let negativeNumbers = [-1.3, -0.5, 0.002, 4.5] let decimalNumbers = [45.7, 44.2, 90.0, 23.3] // get minimum elements let minEvenNo = evenNumbers.min()! let minNegNo = negativeNumbers.min()! let minDecNo = decimalNumbers.min()! // print results print(minEvenNo) print(minNegNo) print(minDecNo)
min()
method on the arrays we created. Then, we store the results in some variables.RELATED TAGS
CONTRIBUTOR
View all Courses