[英]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: { $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.")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.