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.

⚠️ Disclaimer: The information provided on LearnWithNeeraj.com regarding Astrology, Numerology, and other topics is for educational and guidance purposes only.

Not Professional Advice: This content should not be used as a substitute for professional medical, legal, or financial advice. Always consult a certified professional for specific concerns.

Guest Authors: This site features articles by various contributors. The views and interpretations expressed are those of the individual authors and do not necessarily reflect the views of the website administrator.

Your destiny is in your hands. Use this information as a map, not a mandate.

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…