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

how to get number of maximum element in array in swift

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

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

ps:发布时的新质量标准 - 太疯狂了......

这就是我要做的事情:

let array = [1, 3, 5]

if let (maxIndex, maxValue) = array.enumerated().max(by: { [=10=].element < .element }) {
    print("The max element is \(maxValue) at index \(maxIndex)")
}
else {
    print("The array is empty, and has no max element or index.")
}