Scala FAQ: How do I find the unique items in a List
, Array
, Vector
, or other Scala sequence?
Use the distinct
method. Here's a simple example using a List
of integers:
scala> val x = List(1,1,1,2,2,3,3) x: List[Int] = List(1, 1, 1, 2, 2, 3, 3) scala> x.distinct res0: List[Int] = List(1, 2, 3)
As you can see, res0
now contains only the unique elements in the list.