simple-exercises/ruby-exercises/easy-1.rb
# Write a method that takes in a string. Your method should return the
# most common letter in the array, and a count of how many times it
# appears.
Write a method that takes in a string. Your method should return the
most common letter in the array, and a count of how many times it
appears.
Write a method that takes in a string . Your method should return the most common letter in the array and a count of how many times it appears .
def most_common_letter(string)
  letter_count = Hash.new(0)
  (0...string.length).each do |i|
    letter_count[string[i]] += 1
  end
  letter = string[0]
  count = letter_count[letter]
  (1...string.length).each do |i|
    if letter_count[string[i]] > count
      count = letter_count[string[i]]
      letter = string[i]
    end
  end
  [letter, count]
end
def most_common_letter ( string ) letter_count = Hash . new ( 0 ) ( 0 ... string . length ) . each do | i | letter_count [ string [ i ] ] += 1 end letter = string [ 0 ] count = letter_count [ letter ] ( 1 ... string . length ) . each do | i | if letter_count [ string [ i ] ] > count count = letter_count [ string [ i ] ] letter = string [ i ] end end [ letter , count ] end
Write a method that takes in a string.
Write a method that takes in a string .
# letter : how many times
string
<not_specific>