Is there a significantly better way to find the most common word in a list (Python only)
Question: Considering a trivial implementation of the problem, I am looking for a
significantly faster way to find the most common word in a Python list. As
part of Python interview I received feedback that this implementation is so
inefficient, that it is basically failure. Later, I tried many algorithms I
found, and only some heapsearch based solutions are a bit faster, but not
overwhelmingly (when scaled to tens of millions of items, heapsearch is about
30% faster; on trivial lengths like thousand, it is almost the same; using
timeit).

    
    
    def stupid(words):
        freqs = {}
        for w in words:
            freqs[w] = freqs.get(w, 0) + 1
        return max(freqs, key=freqs.get)
    

As this is a simple problem and I have some experience (although I am nowhere
algorithms guru or competitive coder) I was surprised.

Of course, I would like to improve my skills and learn that so much better way
of solving the problem, so your input will be appreciated.

Clarification for duplicate status: My point is to find out if there is
actually much (asymptotically) better solution and other similar questions
have picked an answer that is not much better. If this is not enough to make
the question unique, close this question, of course.

**Update**

Thank you all for the input. Regarding the interview situation, I remain with
the impression that hand written search algorithm was expected (that may be
somewhat more efficient) and/or the reviewer was assessing code from the point
of view of another language, with different constant factors. Of course,
everyone can have own standards.

What was important for me was to validate if I am totally clueless (I had the
impression that I am not) or just usually write not the best possible code. It
is still possible that even better algorithm exists, but if it remained hidden
for the community here for a few days, I am fine with that.

I am picking the most upvoted answer - it seems fair to do so, even though
more than one people privided usefull feedback.

**Minor update**

It seems that using defaultdict has a noticeable advantage over using the
'get' method, even if it is statically aliased.


Answer: That sounds like a bad interview question, probably a case of the interviewer
expecting a certain answer. It definitely sounds like s/he didn't clearly
explain what s/he was asking.

Your solution is `O(n)` (where `n = len(words)`), and using a heap doesn't
change that.

There are faster _approximate_ solutions...

