1

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

matanster
  • 15,072
  • 19
  • 88
  • 167
Praveen L
  • 937
  • 6
  • 13

2 Answers2

1

Using map.filter(_._2 == map.values.max) will do the trick.

vindev
  • 2,240
  • 2
  • 13
  • 20
1
val maxValue = map.values.max
map.filter(_._2 == maxValue).toList
Peter Fitch
  • 243
  • 1
  • 9