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
Or use the group by method:
arr = [1,2,4,5,4,7] Hash[arr.group_by{|x|x}.map{|num,arr| [num, arr.size] }]