Is there a function in scala collections to find the max number of occurrence of a value in a list,
Lets say I have a list
L = List("A","B","B","E","B","E","B","B","C","E","B")
output: "B".
I can write a module to calculate this, but I would expect there should be a scala "way" or scala collection function to do this, already. Thanks!
I don't know of a ready-made way to do it, but this is how I would do it:
l.groupBy(i => i).mapValues(_.size).maxBy(_._2)._1
Oh, but note this doesn't handle the case where the mode is not unique!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With