Mega Code Archive
Use hash to count elements in an array
class Array
def count
k=Hash.new(0)
self.each{|x| k[x]+=1 }
k
end
end
meal = %w[spam spam eggs ham eggs spam]
items = meal.count
# items is {"ham" => 1, "spam" => 3, "eggs" => 2}
spams = items["spam"] # 3