Swift: How to find the largest or smallest value in an array? - COFPROG

Swift: How to find the largest or smallest value in an array?

Question: How to find the largest or smallest value in an array in Swift?


// To find the largest value in an array, you can use the max() function

let numbers = [1, 5, 2, 7, 3, 8, 4]

let largest = numbers.max()

print(largest) // 8


// To find the smallest value in an array, you can use the min() function

let smallest = numbers.min()

print(smallest) // 1


Previous
Next Post »

BOOK OF THE DAY