Scala - Remove duplicates from List


/ Published in: Scala
Save to your folder(s)

Recursively remove duplicate elements from a List


Copy this code and paste it in your HTML
  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)

URL: http://abel-perez.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.