Skip to content

Instantly share code, notes, and snippets.

@makotan
Created December 4, 2011 07:06
Show Gist options
  • Save makotan/1429476 to your computer and use it in GitHub Desktop.
Save makotan/1429476 to your computer and use it in GitHub Desktop.
List(1, 2, 3, 1) を Map(1 -> 2, 2 -> 1, 3 -> 1) から最大の数をまとめて
val map = List(1,2,3,4,1,2).foldLeft(Map[Int,Int]().withDefaultValue(0)){
(map,key) =>
map + (key -> (map(key) + 1))
}.foldLeft(List[Int](),0) {
case ((set,c),(k,v)) if(c < v) => (List(k),v)
case ((set,c),(k,v)) if(c == v) => ((k +: set),v)
case ((set,c),(k,v)) => (set,c)
} _1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment