Created
March 7, 2012 07:24
-
-
Save pminkov/1991619 to your computer and use it in GitHub Desktop.
Scala deduping
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
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