How to get a set of all elements that occur multiple times in a list in Scala?

后端 未结 2 545
生来不讨喜
生来不讨喜 2021-01-21 09:38

E.g. for List(1, 1, 1, 2, 3, 3, 4) it would be Set(1, 3), because 1 and 3 are the only elements which occur multiple times.

2条回答
  •  滥情空心
    2021-01-21 10:03

    val s = List(1, 1, 1, 2, 3, 3, 4) // a list with non-unique elements
    (s diff s.distinct) toSet // Set(1, 3)
    

提交回复
热议问题