Transforming an array in Ruby
Some notes:


From the moment you write hash[n] += 1, your solution is not functional anymore.
each_with_object is also imperative, for FP you should use reduce.
reduce works well with linked lists, not so well for generating new arrays. In any case, reduce is a generic abstraction, there is a more specific one to the problem at hand: Enumerable#chunk.


That's how I'd write it:

xs.chunk(&amp;:itself).map { |y, ys| ys.size == 1 ? y : {y =&gt; ys.size} }