Skip to content

Instantly share code, notes, and snippets.

@vin-droid
Created September 24, 2019 12:04
Show Gist options
  • Save vin-droid/430fa72d8112cd777d42cc4cc4906ed0 to your computer and use it in GitHub Desktop.
Save vin-droid/430fa72d8112cd777d42cc4cc4906ed0 to your computer and use it in GitHub Desktop.
count the number occurrence of a character in a string
## Q-1 Can write a method which will count the number occurrence of a character in a string?
## str = 'element'
def occurance(str)
str_arr = str.split('')
str_arr.inject({}) do |counter, i|
if counter.key?(i)
counter[i] +=1
else
counter[i] = 1
end
counter
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment