I have an array [1,2,4,5,4,7]
and I want to find the frequency of each number and store it in a hash. I have this code, but it returns NoMethodError: undefine
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}