I have Map[String,String]
so keys are distinct but most values are repeated.
For example : Map[car-> "This is a car",truck-> "This is a car", fruit ->"This is a fruit"]
So it should return "This is a car"
because it is repeated twice.
I did something like this. Hope it helps.
val j = x.groupBy(_._2)
Then
j.maxBy(_._2.size)
Where x is your original map. The first call, returns a Map and then you just get the key value pair where the value (map , has max entries)
val m1 = Map("this" -> "that", "what" -> "that", "who" -> "me", "you" -> "who")
m1.groupBy(_._2).maxBy(_._2.size)
res0: ... = (that,Map(this -> that, what -> that))
Another solution
map.values.groupBy(t => t ).values.maxBy(_.size).head
Not the most elegant but my solution goes like
val list = Map(car-> "This is a car",truck-> "This is a car", fruit ->"This is a fruit")
list.map{
case (k,v) => if(list.filter{case (key,value)=> value==v }.size>1)v
}.toSet
"This is a fruit"
be returned if repeated?