Trying to get a count of duplicate elements

Question:

I am new to python and I am just trying to get a count of duplicate elements. My code looks something like this:

n=int(input("Enter the number of products to be stored in a list : "))

list1=[]

for i in range(n):
  items=int(input("value for product " + str(i+1) + " : "))
    list1.append(items)
    
dup=[]

for a in list1:
    if list1.count(a)>1:
        dup.append(a)

print("Count of duplicate elements in the list: ",dup)

Output:

Enter the number of products in a list : 5
value for product 1 : 4
value for product 2 : 4
value for product 3 : 4
value for product 4 : 1
value for product 5 : 5
Count of duplicate elements in the list:  [4, 4, 4]

The answer should be 1 because it is just the number 4 that is a duplicate value but I get the above output. Can someone please advice?

Asked By: Agnel

||

Answers:

You’re adding the duplicate to dup even if it’s already in the list. You should check for that before adding it.

for a in list1:
    if list1.count(a)>1 and a not in dup:
        dup.append(a)

then if you want the count of duplicates, you should print the length of dup, not its contents.

print("Count of duplicate elements in the list: ",len(dup))

You could also make dup a set instead of a list. Then duplicates are ignored. Do this with the initialization:

dup = set()

and use .add() instead of .append().

Answered By: Barmar

If the Count is greater than one, check if a is not in the dup list, then add a to it.
Finally, print the length of the dup list

n=int(input("Enter the number of products to be stored in a list : "))

list1=[]

for i in range(n):
    item = int(input("value for product " + str(i+1) + " : "))
    list1.append(item)
    
dup = []

for a in list1:
    if (list1.count(a) > 1) and (a not in dup):
        dup.append(a)

print("Count of duplicate elements in the list: ", len(dup))

Answered By: Amir Aref

For what it’s worth, here’s a more advanced solution:

Generally for counting the occurrences of multiple elements, it’s easiest to use a Counter. Once you have that, you just need to get the number of items that occur more than once.

list1 = [4, 4, 4, 1, 5]
# ---
from collections import Counter

counts = Counter(list1)  # > Counter({4: 3, 1: 1, 5: 1})
total = sum(count>1 for count in counts.values())

print(total)  # -> 1

This sum() works because count>1 returns False or True, and False == 0 and True == 1.

Answered By: wjandrea

With the numpy library, you could use numpy.unique to count every occurrence in your numpy array:

import numpy

for a in list1: 
    if list1.count(a)>1:
        dup.append(a)

dup_array = numpy.array(dup)
unique, counts = numpy.unique(dup, return_counts=True)
dict(zip(unique, counts))
Answered By: Ruggero

An option could be to use a dict, so you then not only have the count of duplicates, but also what was duplicated and with how many occurrences.

n = int(input("Enter the number of products to be stored in a list : "))  # 

I have entered 5

list1 = []

for i in range(n):
    items = int(input("value for product " + str(i + 1) + " : "))
    list1.append(items)

dup = {}

for a in list1:
    if list1.count(a) > 1:
        dup[a] = dup.get(a, 0)+1

print("Count of duplicate elements in the list: ", len(dup))
print(dup)

Result is:

Enter the number of products to be stored in a list : 5
value for product 1 : 4
value for product 2 : 4
value for product 3 : 4
value for product 4 : 2
value for product 5 : 2
Count of duplicate elements in the list:  2
{4: 3, 2: 2}
Answered By: Gannon

It is simple. You can use Counter from collections to count the occurrences of each element in the list. Then you can count the elements which are not unique as follows.

from collections import Counter
n=int(input("Enter the number of products to be stored in a list : "))

list1=[]

for i in range(n):
    items=int(input("value for product " + str(i+1) + " : "))
    list1.append(items)
dup_dict=Counter(list1)
count=0
for i in dup_dict.values():
    if(i!=1):
        count+=1

print("Count of duplicate elements in the list: ",count)
Answered By: Nishani Kasineshan
`
input=list("AABBCCDDEE")
input1=set(input)
dict1=dict.fromkeys(input1,0)
print("Hello world")
for i in input:
    dict1[i]=dict1[i]+1
print(dict1)`
Answered By: Keshav Kumar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.