# How to Find Duplicate Sets in a List

Given the list below, I would like to find the sets within that list that are identical according to the order of their elements, generate that set, and show the number of times it appears in Lst. For example, in the below the identical sets are {N20029,N31055} which appears 2 times and {N19155,N31055} which appears 3 times.

Lst={{N20029,N31055},{N19155,N31055},{N18163,N31055},{N19155,N31055},{N18017,N31055},{N17149,N31055},{N17111,N31055},{N19155,N31055},{N20029,N31055}}


Tally gives you the count, and select picks only those that occur more than once.

Select[Tally[Lst], #[[2]] > 1 &]

{{{N20029, N31055}, 2}, {{N19155, N31055}, 3}}

• bill s: Thank you very much!!! ... prg Commented Jun 8, 2020 at 17:04

Counts function also can be used for finding duplicates:

Select[Counts[Lst], # > 1 &]

<|{N20029, N31055} -> 2, {N19155, N31055} -> 3|>

• very clever!! ... nice idea too! ... prg Commented Jun 8, 2020 at 19:09
list =
{{N20029, N31055}, {N19155, N31055}, {N18163, N31055},
{N19155, N31055}, {N18017, N31055}, {N17149, N31055},
{N17111, N31055}, {N19155, N31055}, {N20029, N31055}};


Using SequenceCases

SequenceCases[Sort[list], a : {b_, b_ ..} :> {b, Length[a]}]


{{{N19155, N31055}, 3}, {{N20029, N31055}, 2}}

Using Cases and Tally

Cases[Tally @ list, {_, Except @ 1}]


{{{N20029, N31055}, 2}, {{N19155, N31055}, 3}}

PositionIndex[Lst]//Cases[a_:>Rule@@a/;a[[2]]>1][Transpose[{Keys[#],Length/@Values[#]}]]&

{{N20029, N31055} -> 2, {N19155, N31055} -> 3}


Or

PositionIndex[Lst]//Cases[a_/;a[[2]]>1][Transpose[{Keys[#],Length/@Values[#]}]]&

{{{N20029, N31055}, 2}, {{N19155, N31055}, 3}}

list =
{{N20029, N31055}, {N19155, N31055}, {N18163, N31055},
{N19155, N31055}, {N18017, N31055}, {N17149, N31055},
{N17111, N31055}, {N19155, N31055}, {N20029, N31055}};


Using the third argument of GroupBy:

f = {{#1, #2[[1]]}, Length@#2} & @@@ # &;

f@Select[KeyValueMap[List, #] &@GroupBy[lst, First -> Last], Length@#[[2]] > 1 &]


{{{N20029, N31055}, 2}, {{N19155, N31055}, 3}}