In Scala how do I remove duplicates from a list?

前端 未结 8 618
旧巷少年郎
旧巷少年郎 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:30

    Before using Kitpon's solution, think about using a Set rather than a List, it ensures each element is unique.

    As most list operations (foreach, map, filter, ...) are the same for sets and lists, changing collection could be very easy in the code.

提交回复
热议问题