arr = [1,2,3,1,5,2]
arr.select { |x| arr.count(x) > 1 } .uniq
A longer solution using reduce
should be quicker.
arr.reduce [{}, []] do |(seen, out), cur|
case seen[cur]
when :seen then
[seen.merge({cur => :added}), out << cur]
when :added then
[seen, out]
else
[seen.merge({cur => :seen}), out]
end
end.last