From: "David A. Black" Date: 2007-08-22T01:26:04+09:00 Subject: Re: Detecting duplicates in an array, anything in the standa --1926193751-1964928007-1187713559=:30298 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1964928007-1187713559=:30298" 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-1964928007-1187713559=:30298 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Wed, 22 Aug 2007, Phrogz wrote: > On Aug 21, 10:04 am, Ari Brown wrote: >> On Aug 21, 2007, at 4:59 AM, Pe=F1a, Botp wrote: >> >>> From: Robert Klemme [mailto:shortcut...@googlemail.com] >>> # irb(main):002:0> a.uniq.select{|e| (a-[e]).size < a.size - 1} >> >>> compare also, >> >>> irb(main):056:0> b=3Da.dup >>> =3D> [1, 1, 2, 2, 2, 4, 3] >>> irb(main):057:0> b.uniq.select{|e| (b.reject!{|f| f =3D=3D e}).size > 1= } >>> =3D> [1, 2] >> >> I still think it's easier just to union itself... >> >> a =3D [1,2,3,2,1] >> b =3D a & a >> b =3D [1,2,3] > > ...but that's not what the OP wanted. What you've written is the same > as the #uniq method. > > Don't feel bad, this thread has been filled with people answering the > wrong question. :p The original question was roughly "How do I find > out all the elements in the array that are duplicates?" > > Solutions to that question would not include '3' in the above results. > It's unclear to me if %w| a b b b | should include 'b' once or twice > in the output, though, and the original poster has not clarified that, > that I can see. I think once, since it's just the quality of being non-unique in the array that qualifies an object for inclusion. At least, that's my understanding, though as one of the people who reimplemented Array#uniq, I may not be the right person to listen to :-) 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-1964928007-1187713559=:30298-- --1926193751-1964928007-1187713559=:30298--