Created
August 22, 2012 06:05
-
-
Save naveedehmad/3422758 to your computer and use it in GitHub Desktop.
Number of times each character used in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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