The python implementation returns the most frequent element method in a list
- 2021-06-28 13:00:23
- OfStack
As follows:
# Return 1 The most frequent element in a list
def showmax(lt):
index1 = 0 # Element subscripts that record the most occurrences
max = 0 # Record the maximum number of occurrences of an element
for i in range(len(lt)):
flag = 0 # Record every 1 Number of occurrences of elements
for j in range(i+1,len(lt)): # ergodic i Subscript of element after
if lt[j] == lt[i]:
flag += 1 # Whenever you find the same elements as yourself, flag+1
if flag > max: # If the number of occurrences of an element at this time is greater than the maximum, record the subscript of the element at this time
max = flag
index1 = i
return lt[index1] # Return the most occurring element
lt = [1,1,2,3,3,5,6,8,9,4,6,18,6,44,6,44,44,44]
print(showmax(lt))