Get first letter with maximum occurence of a string
<p>I would like to get the first letter with the maximum occurence of a string.</p>

<p>For instance:</p>

<pre><code> "google" -&gt; g  
 "azerty" -&gt; a  
 "bbbaaa" -&gt; b
</code></pre>

<p>I already have a working code, using <a href="https://docs.python.org/3/library/collections.html#collections.Counter">OrdererDict()</a> to avoid automatic keys rearangement:</p>

<pre><code>from collections import OrderedDict

sentence = "google"

d = OrderedDict()

for letter in sentence:
    if letter not in d.keys():
        d[letter] = sentence.count(letter)

print(max(d, key=d.get)) # g
</code></pre>

<p>but I'm looking for a possible one-liner or more elegant solution (if it's possible).</p>

<p><strong>Note:</strong> 
I already tried to use <a href="https://docs.python.org/3/library/collections.html#collections.Counter">Counter()</a> but it doesn't work, since dict in python don't remember the order that keys were inserted. </p>

<p>e.g</p>

<pre><code>from collections import Counter

sentence = "bbbaaa"

c = Counter(sentence)
print(c.most_common()[0][0]) # have 50% chances of printing 'a' rather than 'b'.
</code></pre>

<p>Bonus question: Can someone explains why OrderedDict() are not the default dictionary behavior in python?</p>

<p>Probably not very fast, but one-liner!</p>

<pre><code>&gt;&gt;&gt; s = "aaabbbbc"
&gt;&gt;&gt; sorted(s, key=lambda c: (-s.count(c), s.index(c)))[0]
'b'
</code></pre>

<p><strong>Edit</strong></p>

<p>Even shorter, thanks to @Ohad Eytan's comment:</p>

<pre><code>&gt;&gt;&gt; min(s, key=lambda c: (-s.count(c), s.index(c)))
'b'
</code></pre>

<p><strong>Benchmark</strong></p>

<p>Feeling bored tonight, so I benchmarked (using <code>timeit</code>) test @Joohwan's <code>most_common_char()</code> solution (mostcc), @Blender's <code>OrderedCounter</code> solution (odict) and my own one liner solution (onelin, using the <code>min</code> variant). The fastest solution was consistently mostcc: up to ~10x faster than onelin for long strings containing few different characters, and up to ~4x faster than odict for very short strings. For short strings or strings with little repeated chars, onelin beats odict (otherwise, it's the reverse). Here are the details (Length=length of the string, #chars=number of different unicode chars to randomly pick from for each char, mostcc=time to execute 10,000 times mostcc, odict=how much longer odict was compared to mostcc, onelin=how much longer oneline was compared to mostcc).</p>

<pre><code>Length  #chars  mostcc odict  onelin
10      10:     0.08s  3.76x  1.61x
10      100:    0.10s  3.57x  1.27x
10      1000:   0.12s  3.12x  1.34x
100     10:     0.43s  1.96x  3.29x
100     100:    0.59s  2.16x  2.18x
100     1000:   0.80s  1.92x  1.72x
1000    10:     3.48s  1.56x  9.79x
1000    100:    3.44s  1.72x  6.43x
1000    1000:   6.55s  1.68x  3.30x
</code></pre>

python string python-3.x dictionary