如何在swift中获取数组中的最大元素数

时间:2017-06-18 16:53:29

标签: arrays swift max element

如何解决?在这种情况下,我应该得到2。

var r = [1, 3, 5]
r.max() // gives 5

ps:发布时的新品质标准 - 真是疯狂......

1 个答案:

答案 0 :(得分:1)

这就是我要做的事情:

let array = [1, 3, 5]

if let (maxIndex, maxValue) = array.enumerated.max{ $0.element < $1.element } {
    print("The max element is \(maxValue) at index \(maxIndex)")
}
else {
    print("The array is empty, and has no max element or index.")
}