がんばんなくても
h = {}
a.sort.each_with_index{|e, i| h[e] = i if !h[e]}
                     ~~~~~~