What is an efficient way to find the most common element in a Python list?
My list items may not be hashable so can't use a dictionary. Also in case of draws the item with the lowest index should be returned. Example:
>>> most_common(['duck', 'duck', 'goose'])
'duck'
>>> most_common(['goose', 'duck', 'duck', 'goose'])
'goose'
You probably don't need this anymore, but this is what I did for a similar problem. (It looks longer than it is because of the comments.)
Here:
I have a vague feeling there is a method somewhere in the standard library that will give you the count of each element, but I can't find it.
Sort a copy of the list and find the longest run. You can decorate the list before sorting it with the index of each element, and then choose the run that starts with the lowest index in the case of a tie.
If they are not hashable, you can sort them and do a single loop over the result counting the items (identical items will be next to each other). But it might be faster to make them hashable and use a dict.
This is an O(n) solution.
(reversed is used to make sure that it returns the lowest index item)
I needed to do this in a recent program. I'll admit it, I couldn't understand Alex's answer, so this is what I ended up with.
I timed it against Alex's solution and it's about 10-15% faster for short lists, but once you go over 100 elements or more (tested up to 200000) it's about 20% slower.