Array to Hash : words count
With inject
:
str = 'I have array of words and I want to get a hash, where keys are words'
result = str.split.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
=> {"I"=>2, "have"=>1, "array"=>1, "of"=>1, "words"=>2, "and"=>1, "want"=>1, "to"=>1, "get"=>1, "a"=>1, "hash,"=>1, "where"=>1, "keys"=>1, "are"=>1}
I don't know about the efficiency.
The imperative approach you used is probably the fastest implementation in Ruby. With a bit of refactoring, you can write a one-liner:
wf = Hash.new(0).tap { |h| words.each { |word| h[word] += 1 } }
Another imperative approach using Enumerable#each_with_object
:
wf = words.each_with_object(Hash.new(0)) { |word, acc| acc[word] += 1 }
A functional/immutable approach using existing abstractions:
wf = words.group_by(&:itself).map { |w, ws| [w, ws.length] }.to_h
Note that this is still O(n) in time, but it traverses the collection three times and creates two intermediate objects along the way.
Finally: a frequency counter/histogram is a common abstraction that you'll find in some libraries like Facets: Enumerable#frequency.
require 'facets'
wf = words.frequency
Posted on a related question, but posting here for visibility as well:
Ruby 2.7 onwards will have the Enumerable#tally
method that will solve this.
From the trunk documentation:
Tallys the collection. Returns a hash where the keys are the elements and the values are numbers of elements in the collection that correspond to the key.
["a", "b", "c", "b"].tally #=> {"a"=>1, "b"=>2, "c"=>1}