Easily count occurences in an Array
Counting occurrence in an Array is ezpz with reduce and Hash.new(0).
%w[dog cat dog dog].reduce(Hash.new(0)) do |pet, hash|
hash[pet] += 1
end
=> {"dog"=>3, "cat"=>1}
Counting occurrence in an Array is ezpz with reduce and Hash.new(0).
%w[dog cat dog dog].reduce(Hash.new(0)) do |pet, hash|
hash[pet] += 1
end
=> {"dog"=>3, "cat"=>1}