【问题标题】:how to get number of maximum element in array in swift如何快速获取数组中最大元素的数量
【发布时间】:2017-06-18 16:53:29
【问题描述】:

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

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

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

【问题讨论】:

标签: arrays swift max element


【解决方案1】:

我会这样做:

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.")
}

【讨论】:

    猜你喜欢
    • 2016-12-12
    • 1970-01-01
    • 2012-03-22
    • 2010-11-25
    • 2022-08-16
    • 2012-11-15
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    相关资源
    最近更新 更多