I have a list of tuples like below
List((a,1), (a,2), (b,2), (b,1))
I want to create a List like below from that above list
List((a,2), (b,2))
Only one first element and MAX of the second element. How can I do this concisely?
I have a list of tuples like below
List((a,1), (a,2), (b,2), (b,1))
I want to create a List like below from that above list
List((a,2), (b,2))
Only one first element and MAX of the second element. How can I do this concisely?
Since you only need the 1st elements of same value once along with the max value of the corresponding 2nd element, it's no difference from rephrasing the question as for every distinct 1st element in the tuple list, list the max 2nd element:
val l = List(("a",1), ("a",2), ("b",2), ("b",1))
l.groupBy(_._1).
mapValues( _.map(_._2).max ).
toList
// res1: List[(String, Int)] = List((b,2), (a,2))