Hi,

Using "inject" really isn't a good choice here. If at all, I'd use
"each_with_object" to build up the hash.

If you don't need the whole hash at once, you could also use the block
form of Hash.new to calculate the elements on demand:

arr = [1, 2, 1, 1]

count_hash = Hash.new do |hash, key|
  cnt = arr.count key
  cnt == 0 ? nil : hash[key] = cnt
end

p count_hash[1]

Or you could do something like this:

arr = [1, 2, 1, 1]

arr_uniq = arr.uniq
count_hash = Hash[arr_uniq.zip arr_uniq.map {|e| arr.count e}]

p count_hash

-- 
Posted via http://www.ruby-forum.com/.