Python – find most repeated element in a list

Problem statement: To find out the most repeated element element in a list using Python. Or in other words, to extract the most frequent element in the list without using an inbuilt function. The only allowed python methods are range, print and len.

Solution

list1 = [10, 3, 1, 10, 1, 3, 88, 1, 3, 47, 4, 34, 3, 5]
list2 = []
freq_cntr = 0
most_repeated = list1[0]

def count_elem(elem):
    cntr = 0
    for j in list1:
        if(elem == j):
            cntr+=1
    return cntr


for i in list1:
    elem_repeats = count_elem(i) 
    if(elem_repeats > freq_cntr):
        freq_cntr = elem_repeats
        most_repeated = i
        
print(most_repeated, freq_cntr)
The output is given below:
3 4
where 3 is the most repeated element in the list and 4 represents the number of times it appear in the list.

Other python problems with solution:

Python basic problems

Python Iterate a list : Sample Problem

Learn MongoDB with my following detailed video series

MongoDB Blogs

AWS learning resources over my YouTube channel.

Related Posts

Python Iterate a list : Sample Problem

Iteration is a key concept and it represents to do a specific task repeatedly. Iterations can be executed based on certain conditions. We can implement iterations in…

Leave a Reply

Your email address will not be published. Required fields are marked *