4
$\begingroup$

What is the quickest way to count the maximum number of duplicates in a list? For example, the maximum number of duplicates in {1,1,1,3} is 3, in {1,1,2,2} is 2 and in {1,1,1,2,2,2,3,3} is 3. I've unsuccessfully tried combinations of Commonest, Count and CountDistinct. Other questions have dealt with identifying or deleting duplicates rather than counting them.

$\endgroup$
1
  • 2
    $\begingroup$ Look up Tally. For example, MaximalBy[Tally[lst], Last]. $\endgroup$ – Henrik Schumacher Jun 1 '19 at 12:03
4
$\begingroup$
lst = {1, 1, 1, 2, 2, 2, 3, 3};

ClearAll[f]
f = Max[Counts @ #] /. 1 -> 0 &;

f @ lst

3

f @ {1,2,3,4}

0

$\endgroup$
2
  • 2
    $\begingroup$ The problem is that e.g. {1,2,3} returns 1, but it contains no duplicates (defined as two or more ocurrences). $\endgroup$ – spaced Jun 1 '19 at 12:33
  • $\begingroup$ @spaced, good catch. Updated to get 0 when there are no duplicates. $\endgroup$ – kglr Jun 1 '19 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.