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: undefined method '+' for nil:NilClass
def score( array )
hash = {}
array.each{|key| hash[key] += 1}
end
Desired output is
{1 => 1, 2 => 1, 4 => 2, 5 => 1, 7 => 1 }
Or use the group by method: