On Oct 15, 2005, at 12:56 PM, 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.
>
> a = [0,1,2,3,4,5,2,3]
>
> #What I want to get is [2,3] as 2,3 are non-unique elements.

Here's what I thought of:

 >> a = [0,1,2,3,4,5,2,3]
=> [0, 1, 2, 3, 4, 5, 2, 3]
 >> seen = Hash.new(0)
=> {}
 >> a.select { |e| (seen[e] += 1) > 1 }.uniq
=> [2, 3]

Hope that helps.

James Edward Gray II