【发布时间】:2017-06-18 16:53:29
【问题描述】:
如何解决?在这种情况下,我应该得到 2。
var r = [1, 3, 5]
r.max() // gives 5
ps:发布时的新质量标准 - 太疯狂了...
【问题讨论】:
-
我不明白。是因为 1 和 5 的缘故吗?
-
@AaronZheng He 正在寻找索引
如何解决?在这种情况下,我应该得到 2。
var r = [1, 3, 5]
r.max() // gives 5
ps:发布时的新质量标准 - 太疯狂了...
【问题讨论】:
我会这样做:
let array = [1, 3, 5]
if let (maxIndex, maxValue) = array.enumerated().max(by: { $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.")
}
【讨论】: