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
Love me some inject:
results = array.inject(Hash.new(0)) {|hash, arr_element| hash[arr_element] += 1; hash }
1.9.3p448 :082 > array = [1,2,4,5,4,7]
=> [1, 2, 4, 5, 4, 7]
1.9.3p448 :083 > results = array.inject(Hash.new(0)) {|hash, arr_element| hash[arr_element] += 1; hash }
=> {1=>1, 2=>1, 4=>2, 5=>1, 7=>1}