0
0
RubyProgramBeginner · 2 min read

Ruby Program to Find Most Frequent Character

Use string.chars.group_by(&:itself).transform_values(&:count).max_by(&:last).first in Ruby to find the most frequent character in a string.
📋

Examples

Inputhello
Outputl
Inputmississippi
Outputi
Inputa
Outputa
🧠

How to Think About It

To find the most frequent character, first split the string into characters, then count how many times each character appears. Finally, find the character with the highest count.
📐

Algorithm

1
Get the input string.
2
Split the string into individual characters.
3
Count how many times each character appears.
4
Find the character with the highest count.
5
Return that character.
💻

Code

ruby
def most_frequent_char(str)
  str.chars.group_by(&:itself).transform_values(&:count).max_by(&:last).first
end

puts most_frequent_char("hello")
Output
l
🔍

Dry Run

Let's trace the string "hello" through the code

1

Split string into characters

["h", "e", "l", "l", "o"]

2

Group characters by themselves

{"h"=>["h"], "e"=>["e"], "l"=>["l", "l"], "o"=>["o"]}

3

Count occurrences of each character

{"h"=>1, "e"=>1, "l"=>2, "o"=>1}

4

Find character with max count

["l", 2]

5

Return the character

"l"

CharacterCount
h1
e1
l2
o1
💡

Why This Works

Step 1: Split string into characters

Using chars breaks the string into an array of single characters to work with each one.

Step 2: Group characters

Using group_by(&:itself) groups identical characters together in a hash.

Step 3: Count each group

Using transform_values(&:count) counts how many times each character appears.

Step 4: Find max count

Using max_by(&:last) finds the character with the highest count.

🔄

Alternative Approaches

Using a frequency hash with iteration
ruby
def most_frequent_char(str)
  freq = Hash.new(0)
  str.each_char { |c| freq[c] += 1 }
  freq.max_by { |_, count| count }[0]
end

puts most_frequent_char("hello")
This method uses manual counting with a hash and is easy to understand but slightly longer.
Using Enumerable#tally (Ruby 2.7+)
ruby
def most_frequent_char(str)
  str.chars.tally.max_by { |_, count| count }[0]
end

puts most_frequent_char("hello")
This is the shortest and most readable method using Ruby's built-in tally method.

Complexity: O(n) time, O(k) space

Time Complexity

The program loops through each character once to count frequencies, so it runs in O(n) time where n is the string length.

Space Complexity

It uses extra space to store counts for each unique character, which is O(k) where k is the number of unique characters.

Which Approach is Fastest?

Using tally is fastest and most readable, while manual hash counting is more verbose but clear.

ApproachTimeSpaceBest For
group_by + transform_valuesO(n)O(k)Readable grouping and counting
manual hash countingO(n)O(k)Explicit counting, beginner-friendly
Enumerable#tallyO(n)O(k)Shortest and most efficient for Ruby 2.7+
💡
Use Ruby's tally method for a clean and efficient way to count characters.
⚠️
Forgetting to handle empty strings or assuming the input always has characters.