Archer Archer - 1 year ago 94
Scala Question

Get key with lowest value

I am trying to get the key with the lowest value in the following.

The answer should return: 12

res14: Array[(String, Int)] = Array((8,33), (15,347), (5,2257), (2,18117), (16,955), (9,33), (3,1126), (12,18), (1,1009), (11,25), (10,397))

Would anyone know how I might go about doing this?

Thank you.

Answer Source

You can use reduce for example:

keyCount.collect().reduce((l, r) => if (l._2 < r._2) l else r)._1

or minBy

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download