Skip to content

Instantly share code, notes, and snippets.

@rkrzewski
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save rkrzewski/c5c2dcb0c5d08fb21a0b to your computer and use it in GitHub Desktop.

Select an option

Save rkrzewski/c5c2dcb0c5d08fb21a0b to your computer and use it in GitHub Desktop.
def mostCommon(l: ParSeq[Int]): Int = {
type Freq = Map[Int, Int]
def addToFreq(freq: Freq, v: Int, f: Int) =
freq.updated(v, freq.getOrElse(v, 0) + f)
def mapFreq(freq: Freq, v: Int) =
addToFreq(freq, v, 1)
def reduceFreq(freq1: Freq, freq2: Freq) =
freq1.foldLeft(freq2) { case (freq, (v, f)) => addToFreq(freq, v, f) }
val freq = l.aggregate(Map[Int, Int]())(mapFreq, reduceFreq)
freq.toSeq.maxBy(_._2)._1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment