From: Simon Strandgaard Date: 2005-10-18T03:15:46+09:00 Subject: Re: How to get non-unique elements from an array? On 10/17/05, Ryan Leavengood wrote: > On 10/17/05, Simon Strandgaard wrote: > > On 10/17/05, pauldacus@gmail.com wrote: > > > Young grasshoppers... let us never forget the power of the REGEX! Let > > > us not bother with counters or blocks... let us GSUB this bad boy! > > > > > > a = [0,1,2,3,4,5,2,3] > > > > > > a.sort.to_s.gsub(/(.)(\1)+/, '').split(//) > > > > > > =>["0", "1", "4", "5"] > > > > > > no need to sort.. :-) > > > > a = [0, 1, 2, 3, 4, 5, 2, 3] > > a.to_s.delete(a.to_s.gsub(/(.)(?!.*\1)/,'')).split('').map{|i|i.to_i} > > #=> [0, 1, 4, 5] > > Both are interesting solutions, but you are returning the unique > elements of the array, not the non-unique ones, which was the original > problem. I say this not to be pedantic, but to see how you would solve > it. Sorry should be without the delete.. a = [0, 1, 2, 3, 4, 5, 2, 3] a.to_s.gsub(/(.)(?!.*\1)/,'').split('').map{|i|i.to_i} #=> [2, 3] -- Simon Strandgaard