How do you find a maximum value in a Swift dictionary?

后端 未结 5 1382
礼貌的吻别
礼貌的吻别 2020-12-14 08:51

So, say I have a dictionary that looks like this:

var data : [Float:Float] = [0:0,1:1,2:1.414,3:2.732,4:2,5:5.236,6:3.469,7:2.693,8:5.828,9:3.201]

5条回答
  •  星月不相逢
    2020-12-14 09:34

    A Swift Dictionary provides the max(by:) method. The Example from Apple is as follows:

    let hues = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]
    let greatestHue = hues.max { a, b in a.value < b.value }
    print(greatestHue)
    // Prints "Optional(("Heliotrope", 296))"
    

提交回复
热议问题