Alpha-Curriculum, Hashes Walk-Through Error


While reviewing App Academy’s solutions to the Hash exercises, I noticed that the given method would not work for “aeeooauuii”.

According to the description below, the answer should be “a”.

Define a method that, given a string, returns the most common vowel. If there’s a tie, return the vowel that occurs earlier in the alphabet. Assume all letters are lower case.

The following is the given method and it’s return value:

def most_common_vowel(string)
  vowels = %w(a e i o u)
  counts = # Give the hash a default value of 0

  string.each_char do |character|
    counts[character] += 1 if vowels.include?(character)

  counts.sort_by {|k, v| v}.last.first

p most_common_vowel("aeeooauuii") => "i"

I’m not sure how “counts.sort_by {|k, v| v}.last.first” would return the earliest key if there is a tie between values.