Friday, 3 May 2019

Finding the repeated words count from an array in ruby

a = %w(hi this is rajkumar this is uthayaa this is testing for repeat and repeat string)
b = Hash.new 0
a.each do |w|
  b[w] += 1
end
puts b

output: => {"hi"=>1, "this"=>3, "is"=>3, "rajkumar"=>1, "uthayaa"=>1, "testing"=>1, "for"=>1, "repeat"=>2, "and"=>1, "string"=>1}

No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...