Skip to content

Instantly share code, notes, and snippets.

@naveedehmad
Created August 22, 2012 06:05
Show Gist options
  • Save naveedehmad/3422758 to your computer and use it in GitHub Desktop.
Save naveedehmad/3422758 to your computer and use it in GitHub Desktop.
Number of times each character used in a string
def count_chars(input_string)
char_count_map = {}
char_list = []
input_string.chars.each do |ch|
if not char_count_map.has_key?(ch)
char_list << ch
char_count_map[ch] = 0
end
char_count_map[ch] += 1
end
char_list.each do |c|
puts "#{c}: (#{char_count_map[c]}) times."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment