Created
September 24, 2019 12:04
-
-
Save vin-droid/430fa72d8112cd777d42cc4cc4906ed0 to your computer and use it in GitHub Desktop.
count the number occurrence of a character 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
## 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