+3 votes
in Programming Languages by (71.8k points)

I want to find the list element with minimum occurrences. Is there any Python function for it?

E.g.

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

Answer = 12

1 Answer

+4 votes
by (349k points)
selected by
 
Best answer

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

Using min() function:

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

Using Counter of collections:

>>> from collections import Counter
>>> Counter(a).most_common()[-1][0]
12

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][-1]
12
>>>


...