Posted By

abelperez on 04/09/11


Tagged

duplicates foreach recursion Scala


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tjikuzu2006


Scala - Remove duplicates from List


 / Published in: Scala
 

URL: http://abel-perez.com

Recursively remove duplicate elements from a List

  1. object ListUtil
  2. {
  3. def dedupe(elements:List[String]):List[String] = {
  4. if (elements.isEmpty)
  5. elements
  6. elements.head :: dedupe(for (x <- elements.tail if x != elements.head) yield x)
  7. }
  8. }
  9.  
  10. // example usage:
  11. ListUtil.dedupe(List("one", "two", "one")).foreach(println)

Report this snippet  

You need to login to post a comment.