How in Scala to find unique items in List?
list.filter { x => list.count(_ == x) == 1 }
Roll your own uniq filter with order retention:
scala> val l = List(1,2,3,3,4,6,5,6)
l: List[Int] = List(1, 2, 3, 3, 4, 6, 5, 6)
scala> l.foldLeft(Nil: List[Int]) {(acc, next) => if (acc contains next) acc else next :: acc }.reverse
res0: List[Int] = List(1, 2, 3, 4, 6, 5)
Imho, all the interpretations of the question are false:
How in Scala to find unique items in List?
Given this list:
val ili = List (1, 2, 3, 4, 4, 3, 1, 1, 4, 1)
the only unique item in the list is 2
. The other items aren't unique.
ili.toSet.filter (i => ili.indexOf (i) == ili.lastIndexOf (i))
will find it.
The most efficient order-preserving way of doing this would be to use a Set
as an ancillary data structure:
def unique[A](ls: List[A]) = {
def loop(set: Set[A], ls: List[A]): List[A] = ls match {
case hd :: tail if set contains hd => loop(set, tail)
case hd :: tail => hd :: loop(set + hd, tail)
case Nil => Nil
}
loop(Set(), ls)
}
We can wrap this in some nicer syntax using an implicit conversion:
implicit def listToSyntax[A](ls: List[A]) = new {
def unique = unique(ls)
}
List(1, 1, 2, 3, 4, 5, 4).unique // => List(1, 2, 3, 4, 5)
list.toSet will do it since Set by definition only contains unique elements
A simple ad-hoc method is just to add the List to a Set, and use from there:
val l = List(1,2,3,3,3,4,5,5,6,7,8,8,8,9,9)
val s = Set() ++ x
println(s)
Produces:
> Set(5, 1, 6, 9, 2, 7, 3, 8, 4)
This works for a Seq (or any Iterable), but is not necessary in 2.8, where the removeDuplicates method will probably be more readable. Also, not sure about the runtime performance vs a more thought-out conversion.
Also, note the lost ordering.