From: "David A. Black" Date: 2007-08-19T20:33:37+09:00 Subject: Re: Detecting duplicates in an array, anything in the standa --1926193751-1683877422-1187523218=:13998 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1683877422-1187523218=:13998" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-1683877422-1187523218=:13998 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Sun, 19 Aug 2007, Wolfgang N=C3=A1dasi-Donner wrote: > Thibaut Barr=C3=A8re wrote: >> Anything more elegant ? > > No! :-)) - I tried it only using Arrays... > > a =3D [1,2,3,4,5,4,2,2] > p a.inject([[],a[1..-1]]){|r,e|r[1].include?(e) ? [r[0]< : [r[0], r[1][1..-1]]}[0].uniq # =3D> [2, 4] > b =3D %w(a b c c) > p b.inject([[],b[1..-1]]){|r,e|r[1].include?(e) ? [r[0]< : [r[0], r[1][1..-1]]}[0].uniq # =3D> ["c"] How about: >> a =3D [1,2,3,4,5,4,2,2] =3D> [1, 2, 3, 4, 5, 4, 2, 2] >> a.inject([]) {|acc,e| acc << e unless acc.include?(e); acc } =3D> [1, 2, 3, 4, 5] David --=20 * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com) --1926193751-1683877422-1187523218=:13998-- --1926193751-1683877422-1187523218=:13998--