Archer Archer - 1 month ago 7
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

keyCount.collect()
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

You can use reduce for example:

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

or minBy

keyCount.collect().minBy(_._2)._1
Comments