just do:

array = [ 1, 2 , 1 , 1]

p array.inject({}){|x,y| x[y] += 1 rescue x[y] = 1; x}