View Problem

Categorise a list

Given the list [one, two, three, four, five] produce a map {3:[one, two], 4:[four, five], 5:[three]} which sorts elements into map entries based on their length
ExpandDiskEdit
scala
list foreach { (x) => map += x.length -> (x :: map.getOrElse(x.length, Nil)) }
ExpandDiskEdit
scala
list foreach { (x) => map += x.length -> (map.getOrElse(x.length, Nil) ::: List(x)) }
ExpandDiskEdit
scala
List("one", "two", "three", "four", "five") groupBy (_ size)

Submit a new solution for scala
There are 22 other solutions in additional languages (clojure, cpp, csharp, erlang ...)