Created
April 2, 2014 18:42
-
-
Save pmashchak/9940438 to your computer and use it in GitHub Desktop.
Find occurences in 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
a = 'Baloon . || woo,,ooddreeeed' | |
occurences = Hash.new(0) | |
array = a.gsub(/\W+/, '').split('') | |
array.each_with_index do |el, idx| | |
break if array.size == idx + 1 | |
if el == array[idx + 1] && el != array[idx - 1] | |
occurences[el] += 1 | |
end | |
end | |
occurences.values.reduce(:+) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment