From: Paolo Capriotti
Date: 2005-10-16T03:17:59+09:00
Subject: Re: How to get non-unique elements from an array?
On 10/15/05, Sam Kong wrote:
> Hello!
>
> I need to get non-unique elements from an array.
> The best I came up with was using a hash as a counter for each unique
> elements.
You don't need the exact number of repetitions. Try this:
a = [0,1,2,3,4,5,2,3]
h = {}
u = a.inject([]) {|res, x| h[x] ? res + [x] : h[x] = res}
Paolo