DEV Community

oladejo abdullahi
oladejo abdullahi

Posted on • Updated on

Count Repeated element in the list [duplicate]

Count the number of Repeated element in the list

To count the Repeated element in list is very similar to the way we count the character in a string. here are three methods that can be use to do this.

Method 1 : Naive method

just like we learn in counting character in string we need to loop through the entire List for that particular element and then increase the counter when we meet the element again.

Example

you were given a variable MyList containing some elements you were to count the number of 'a' in the string.
Here is the codes:

MyList = ["b", "a", "a", "c", "b", "a", "c",'a']
count=0
for i in MyList:
    if i == 'a': 
        count = count + 1  
print ("the number of a in MyList is :", count)
Enter fullscreen mode Exit fullscreen mode

Output:

the number of a in MyList is : 4
Enter fullscreen mode Exit fullscreen mode

Method 2 : Using count()

Using count() is the convinient method in Python to get the occurrence of any element in List. the formula to count element in list with this method is:
list.count('element')

Example1

Let say we need to count 'b' in MyList here is the code:

MyList = ["b", "a", "a", "c", "b", "a", "c",'a']
counter_b=MyList.count('b')
print(counter_b)
Enter fullscreen mode Exit fullscreen mode

Output:

2
Enter fullscreen mode Exit fullscreen mode

Example2

what if we wish to count each of the elements in the List. our code will be..

MyList = ["b", "a", "a", "c", "b", "a", "c",'a']
duplicate_dict={} # a dictionary to store each of them.
for i in MyList:#loop through them.
    duplicate_dict[i]=MyList.count(i)
print(duplicate_dict)#to get the occurence of each of the element
Enter fullscreen mode Exit fullscreen mode

Output:

{'b': 2, 'a': 4, 'c': 2}
Enter fullscreen mode Exit fullscreen mode

A shortcuts code for the above are:

MyList = ["b", "a", "a", "c", "b", "a", "c",'a']
duplicate_dict = {i:MyList.count(i) for i in MyList}
print(duplicate_dict)
Enter fullscreen mode Exit fullscreen mode

Output:

{'b': 2, 'a': 4, 'c': 2}
Enter fullscreen mode Exit fullscreen mode

Method3 : Using collections.Counter()

This method works also the same way only that you need to import Counter from the collection before use.
Let's see how to use it to solve the same question

#we need to import counter function.
from collections import Counter
MyList = ["a", "b", "a", "c", "c", "a", "c"]
duplicate_dict = Counter(MyList)
print(duplicate_dict)#to get occurence of each of the element.
print(duplicate_dict['a'])# to get occurence of specific element.
Enter fullscreen mode Exit fullscreen mode

Output:

Counter({'a': 3, 'c': 3, 'b': 1})
3
Enter fullscreen mode Exit fullscreen mode

remember to import the Counter if you are using Method otherwise you get error. I hope you find this helpful, yeah keep on enjoying coding.

if you have any question don't hesitate to ask. chat me up on WhatsApp or Mail. Don't forget to follow me on Twitter so that you don't miss any of my articles.

Top comments (2)

Collapse
 
chemistlabs profile image
Madi GoBuddy

I loved it, thanks....

Collapse
 
maxwizardth profile image
oladejo abdullahi

You are welcome!