Created
February 26, 2014 13:10
-
-
Save chemacortes/9229213 to your computer and use it in GitHub Desktop.
Remove duplicates from list in scala (but use better `list.distinct`)
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
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