How to find duplicates in a list?

后端 未结 5 1639
梦毁少年i
梦毁少年i 2020-12-09 15:25

I have a list of unsorted integers and I want to find those elements which have duplicates.

val dup = List(1,1,1,2,3,4,5,5,6,100,101,101,102         


        
5条回答
  •  粉色の甜心
    2020-12-09 15:45

    A bit late to the party, but here's another approach:

    dup.diff(dup.distinct).distinct
    

    diff gives you all the extra items above those in the argument (dup.distinct), which are the duplicates.

提交回复
热议问题