Reputation: 987
I have seen a similar post here here which is giving a single key-value pair which has maximum value in the entire Map.
But I would like to get List of pairs which has maximum value(maximum value is same for many pairs).
Ex : Map(1 -> 7, 2 -> 1, 4 -> 7, 3 -> 2)
Expected Output : List(1 -> 7, 4 -> 7)
This (Map(1 -> 7, 2 -> 1, 4 -> 7, 3 -> 2).maxBy(x => x._2)
) will give only first occurrence 1 -> 7
Upvotes: 1
Views: 996
Reputation: 253
val maxValue = map.values.max
map.filter(_._2 == maxValue).toList
Upvotes: 1