Take the 2-minute tour ×

I have two list to compare:

List one:


List two:


how can I get the unique values from these two lists?

share|improve this question
What do you mean by "unique values"? Do you want the set union of the two lists, or those elements which only appear in exactly one list? –  oxbow_lakes Jul 19 '12 at 11:42
add comment

4 Answers

up vote 3 down vote accepted

If your two lists are r1 and r2, and assuming you want the values in each list that are not present in the other:

r1.filterNot(r2.contains) ::: r2.filterNot(r1.contains)


r1.diff(r2) ::: r2.diff(r1)
share|improve this answer
add comment

Use The difference operator for Set &~


share|improve this answer
thanks. it helped –  Anuj Mehta Oct 23 '13 at 12:24
add comment

I use List(1, 2, 3, 4) ::: List(1, 2, 5) distinct for this issue. It returns List(1, 2, 3, 4, 5).

share|improve this answer
add comment

Turn them into sets, and get the intersection. You may then turn it back to Seq if you want, but first ask yourself if they had to be Seq in first place, instead of Set.

scala> List("one","two","three","four").toSet & List("one","two").toSet
res0: scala.collection.immutable.Set[String] = Set(one, two)
share|improve this answer
add comment

Your Answer


