From: Mark Van Holstyn Date: 2005-10-18T05:40:40+09:00 Subject: Re: How to get non-unique elements from an array? ------=_Part_86147_19898330.1129581637841 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hey Sam, I too tried a solution similar to... a.select{|e| a.index(e) !=3D a.rindex(e)}.uniq Then i tried the one which i wrote... t=3D[]; a.select{ |e| r=3Dt.include?e; t< 1}.uniq } = } bx.report('scan ') { BMN.times { a.sort.join(' ').to_s.scan(/(\d+)\s(\1)+/).flatten.uniq } } bx.report('inject ') { BMN.times { a.uniq.inject(a.dup) { |b,i| b.delete_at= ( b.index(i)) ; b } } } bx.report('gsub ') { BMN.times { a.to_s.gsub(/(.)(?!.*\1)/,'').split('').map{|i|i.to_i} } } bx.report('include') { BMN.times { t=3D[]; a.select{ |e| r=3Dt.include?e; t= < wrote: > > Hi! > > I'm the OP. > I thank those who replied to my message. > I learned a lot and seeing all different approaches was fun. > > Somebody emailed me with his own solution which I like very much. > I want to share it with the group. > > a =3D [0,1,2,3,4,5,2,3] > a.select{|e| a.index(e) !=3D a.rindex(e)}.uniq > > It is very brief and still very easy to understand. > Don't you think so? > > I wonder if my slightly modified version works. > > a.select{|e| a.grep(e).length > 1}.uniq > > grep is for pattern but seems to work fine for integers. > I'm not sure though. (Simple tests show it works.) > > > In my opinion, using regex is too much for this problem (maybe > slower?). > Array#inject seems to be good but I get confused whenever I come across > it (maybe I'm not smart enough). > > What do you think? > > Thanks all! > > Sam > > > -- Mark Van Holstyn mvette13@gmail.com http://lotswholetime.com ------=_Part_86147_19898330.1129581637841--