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>The documentation for <code>collections.OrderedDict</code> actually has <a href="https://docs.python.org/3/library/collections.html#ordereddict-examples-and-recipes">a recipe for an <code>OrderedCounter</code></a>:</p>

<pre><code>In [5]: from collections import Counter, OrderedDict

In [6]: class OrderedCounter(Counter, OrderedDict):
   ...:     pass
   ...:

In [7]: OrderedCounter("google").most_common()[0][0]
Out[7]: 'g'
</code></pre>

python string python-3.x dictionary