-1

I would like to ask how to count the total number of elements which has duplicated in Python?

For example

Input: [1,2,2,3,4,4,5,6,7]

Output: 2 (because there is 2 elements are duplicated 2 and 4)

gggyyy
  • 17
  • 5

2 Answers2

1

Not entirely clear whether you want the number of elements that have duplicates, or elements that are duplicates. If you want the elements that are duplicates of others, you can just convert to set and get the difference, as suggested in comments. (I added another 4 to the lst to illustrate the difference.)

>>> lst = [1, 2, 2, 3, 4, 4, 4, 5, 6, 7]
>>> len(lst) - len(set(lst))
3

However, I think you want the number of elements that have duplicates, and that is a little more involved. You can just count how often each unique element appears, but that will have complexity O(n²). That's okay for short lists, but not good for lists with thousands of elements.

>>> sum(1 for x in set(lst) if lst.count(x) > 1)
2

Instead, I'd suggest using collections.Counter (or just a dict and a for loop) to count how often each element appears, then get those with a count > 1 in O(n).

>>> from collections import Counter
>>> c = Counter(lst)
>>> sum(1 for x in c if c[x] > 1)
2

If the elements in the list are sorted, you could also use itertools.groupby in O(n), but I'd still prefer the more explicit Counter.

>>> from itertools import groupby
>>> sum(1 for k, g in groupby(lst) if len(list(g)) > 1)
2
tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

Get all duplicates in one list, remove the duplicates from the new list, and get the length.

l = [1,2,2,3,4,4,5,6,7]

m = set([x for x in l if l.count(x) >1])
print(len(m))
coderoftheday
  • 1,987
  • 4
  • 7
  • 21