Skip to content

Instantly share code, notes, and snippets.

@nandhasuhendra
Created March 28, 2021 07:37
Show Gist options
  • Save nandhasuhendra/319c47c2eb61ff669e6ed8b321e0f598 to your computer and use it in GitHub Desktop.
Save nandhasuhendra/319c47c2eb61ff669e6ed8b321e0f598 to your computer and use it in GitHub Desktop.
def counter(string)
{}.tap do |result|
string.each_char { |chr| result[chr] ? result[chr] += 1 : result[chr] = 1 }
end
end
def format_output(hash)
hash.each do |key, total|
puts "Huruf #{key} : #{total}"
end
end
format_output counter('UUPpKooding')
# RESULT:
# Huruf U : 2
# Huruf P : 1
# Huruf p : 1
# Huruf K : 1
# Huruf o : 2
# Huruf d : 1
# Huruf i : 1
# Huruf n : 1
# Huruf g : 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment