In Scala how do I remove duplicates from a list?

前端 未结 8 600
旧巷少年郎
旧巷少年郎 2020-12-13 22:57

Suppose I have

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

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

相关标签:
8条回答
  • 2020-12-13 23:43

    Have a look at the ScalaDoc for Seq,

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

    Update. Others have suggested using Set rather than List. That's fine, but be aware that by default, the Set interface doesn't preserve element order. You may want to use a Set implementation that explicitly does preserve order, such as collection.mutable.LinkedHashSet.

    0 讨论(0)
  • 2020-12-13 23:43

    inArr.distinct foreach println _

    0 讨论(0)
提交回复
热议问题