Max and min in an array of Float in Swift

后端 未结 5 1032
悲哀的现实
悲哀的现实 2021-01-14 17:57

According to this answer, to obtain the maximum of an array we can do:

let nums = [1, 6, 3, 9, 4, 6];
let numMax = nums.reduce(Int.min, { max($0, $1) })
         


        
5条回答
  •  一个人的身影
    2021-01-14 18:33

    Just use the first array element as the initial value:

    let numMax = floats.reduce(floats[0], { max($0, $1) })
    

    but of course you need to check that the floats array is not empty before doing it.

提交回复
热议问题