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 Iterate a list : Sample Problem
Learn MongoDB with my following detailed video series
AWS learning resources over my YouTube channel.