Lobsterrrr Lobsterrrr - 1 year ago 89
Scala Question

How to find most frequent string in List of strings

I have a list of strings (

) and I want to obtain the most frequent string from this list:

val list1 = List('a','a','0','b','b','a')

The answer should be:

freq_list1 = a

I was thinking to use
in order to get the count of unique string, but I don't know how to wrap it into finding the most frequent string.

Answer Source

EDIT: See comment below, can be made shorter by using maxBy(_._2.size) without mapping beforehand, thanks @kawty

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download