Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created April 9, 2011 16:37
Show Gist options
  • Save debasishg/911533 to your computer and use it in GitHub Desktop.
Save debasishg/911533 to your computer and use it in GitHub Desktop.
de-duplicate
def deduplicate[T](l: List[T]): List[T] = {
def deduplicate_acc[T](l: List[T], acc: List[T]): List[T] = l match {
case x :: xs if acc contains x => deduplicate_acc(xs, acc)
case x :: xs => deduplicate_acc(xs, acc :+ x)
case Nil => acc
}
deduplicate_acc(l, List.empty[T])
}
println(deduplicate(List(1,2,2,3,4,5,1,4,6,8,3))) // List(1, 2, 3, 4, 5, 6, 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment