Skip to content

Instantly share code, notes, and snippets.

@pmashchak
Created April 2, 2014 18:42
Show Gist options
  • Save pmashchak/9940438 to your computer and use it in GitHub Desktop.
Save pmashchak/9940438 to your computer and use it in GitHub Desktop.
Find occurences in string
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