Skip to content

Instantly share code, notes, and snippets.

@pminkov
Created March 7, 2012 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pminkov/1991619 to your computer and use it in GitHub Desktop.
Save pminkov/1991619 to your computer and use it in GitHub Desktop.
Scala deduping
val a = List(1, 2, 5)
val b = List(6, 5, 1, 7, 5)
val c = List(2, 8, 16, 8)
val all = List(a, b, c)
val initial = (List.empty[List[Int]], Set.empty[Int])
val (nodups, used) = all.foldLeft(initial)((state, list) => {
val (lists, used) = state
val filteredList = list.filter(!used.contains(_)).distinct
(filteredList +: lists, used ++ list.toSet)
})
nodups.reverse.foreach(li => println("list:" + li.mkString(",")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment