Created
April 9, 2011 16:37
-
-
Save debasishg/911533 to your computer and use it in GitHub Desktop.
de-duplicate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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