Skip to content

Instantly share code, notes, and snippets.

@chemacortes
Created February 26, 2014 13:10
Show Gist options
  • Save chemacortes/9229213 to your computer and use it in GitHub Desktop.
Save chemacortes/9229213 to your computer and use it in GitHub Desktop.
Remove duplicates from list in scala (but use better `list.distinct`)
object ListUtil {
def dedupe[T](elements:List[T]):List[T] = elements match {
case Nil => elements
case head::tail => head :: dedupe(tail filterNot (_==head))
}
}
import ListUtil._
println( dedupe(List("one","two","one")) ) // List(one, two)
println( dedupe(List(1,2,3,4,5,2,3,6,1,1)) ) // List(1, 2, 3, 4, 5, 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment