I have an array with some objects, and there are several objects that are alike. E.g: fruit = [apple, orange, apple, banana, banana, orange, apple, apple]
What is the most efficient way to get the most represented object from this array? In this case it would be “apple” but how would you go out and calculate that in an efficient way?
Don’t reinvent the wheel. In Python 2.7+ you can use the Counter class:
If you are using an older version of Python, then you can download
Counterhere.While it’s good to know how to implement something like this yourself, it’s also a good idea to get used to using Counter, since it is (or going to be) part of the standard library.