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>I am aware that you want a one-liner, but what if you had to repeat this task many times or handle really long sentences? I don't know the exact use case here but it could be worth your time considering the space and time complexity of the algorithm.</p>

<p>In your solution, for example, you are iterating through the sentence many times more than necessary with <code>sentence.count()</code>, which takes <code>O(n * number of unique characters)</code>. After that you iterate through the ordereddict once more to find the max (another <code>O(number of unique characters)</code> operation).</p>

<p>In the accepted solution, we end up having to define a new class (which breaks your 1 liner requirement btw) and instantiate new objects with alot of boilerplate code and functionalities you probably won't need every time you want to execute your task.</p>

<p>If you don't mind having a few more lines of code (again, I know this is not what the question is asking), we can build a reusable function which only has to iterate through the string <strong>once</strong> and use constant and minimal space:</p>

<pre><code>from collections import defaultdict


def most_common_char(sentence):
    if not sentence:
        return ''

    max_count = 1
    max_char = sentence[-1]
    char_counts = defaultdict(int)
    char_counts[max_char] = 1

    for i in xrange(len(sentence) - 2, -1, -1):
        char = sentence[i]
        char_counts[char] += 1
        if char_counts[char] &gt;= max_count:
            max_count = char_counts[char]
            max_char = char

    return max_char
</code></pre>

<p>We keep track of the character with the max count <strong>as</strong> we go through the string and spit it out at the end of the iteration. Note that we iterate <em>backwards</em> since you want the letter that comes first (i.e. last updated wins).</p>

python string python-3.x dictionary