+3 votes
in Programming Languages by (71.8k points)
I want to find the list element with maximum occurrences. Is there any Python function for it?

E.g.

a=[10,11,12,10,10,11,10,11,10,10]

Answer = 10

1 Answer

+3 votes
by (348k points)
selected by
 
Best answer

There are several ways to find the element with maximum occurrences in a given list.

Using max() function:

>>> a=[10,11,12,10,10,11,10,11,10,10]
>>> max(a, key=a.count)
10

Using Counter of collections:

>>> from collections import Counter
>>> Counter(a).most_common()[0][0]
10

Using unique() of numpy:

>>> import numpy as np
>>> np.unique(a, return_counts=True)
(array([10, 11, 12]), array([6, 3, 1]))
>>> np.unique(a, return_counts=True)[0][0]
10


...