In Scala, how to remove duplicates from a list?

Let's pretend that

val dirty = List("a", "b", "a", "c") 

Is there a list operation that returns "a", "b", "c"

+72
scala
Aug 21 2018-11-11T00:
source share
6 answers

Take a look at ScalaDoc for Seq ,

 scala> dirty.distinct res0: List[java.lang.String] = List(a, b, c) 

Update Others suggested using Set rather than List . This is great, but keep in mind that by default the Set interface does not preserve the order of elements. You can use a Set implementation that explicitly preserves order, for example collection.mutable.LinkedHashSet .

+139
Aug 21 2018-11-11T00:
source share

scala.collection.immutable.List now has a .distinct method.

Thus, calling dirty.distinct now possible without conversion to Set or Seq .

+14
Jan 22
source share

Before using the Kitpon solution, consider using Set rather than List , it ensures that each element is unique.

Since most operations with lists ( foreach , map , filter , ...) are the same for sets and lists, changing the collection can be very simple in code.

+13
Aug 21 '11 at 16:17
source share

Using Set in the first place is the right way to do this, of course, but:

 scala> List("a", "b", "a", "c").toSet.toList res1: List[java.lang.String] = List(a, b, c) 

Works. Or just toSet as it supports the interface Seq Traversable .

+4
Aug 21 2018-11-21T00:
source share

inArr.distinct foreach println _

-one
Mar 04 '14 at 23:28
source share

Algorithmic way ...

 def dedupe(str: String): String = { val words = { str split " " }.toList val unique = words.foldLeft[List[String]] (Nil) { (l, s) => { val test = l find { _.toLowerCase == s.toLowerCase } if (test == None) s :: l else l } }.reverse unique mkString " " } 
-four
Sep 21 '15 at 6:37
source share



All Articles