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
[1,2,4,5,4,7]
NoMethodError: undefine
Here is a short option that uses the Hash array initializer
Hash[arr.uniq.map {|v| [v, arr.count(v)] }]