From: "David A. Black" Date: 2007-08-21T20:49:15+09:00 Subject: Re: Detecting duplicates in an array, anything in the standa --1926193751-448758571-1187696956=:25684 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-448758571-1187696956=:25684" 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-448758571-1187696956=:25684 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Tue, 21 Aug 2007, Ryan Davis wrote: > > On Aug 21, 2007, at 01:59 , Pe=F1a, Botp wrote: > >> From: Robert Klemme [mailto:shortcutter@googlemail.com] >> # irb(main):002:0> a.uniq.select{|e| (a-[e]).size < a.size - 1} >>=20 >> compare also, >>=20 >> 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 came up with something vaguely similar: > > class Array > def dupes > a =3D self.dup > self.partition { |o| a.delete(o) }.last > end > end > >>> [1,2,2,3,4,4].dupes > =3D> [2, 4] You'd want to throw a .uniq on there; otherwise, non-consecutive dupes get processed twice: >> [1,2,2,3,4,4,2].dupes =3D> [2, 4, 2] 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-448758571-1187696956=:25684-- --1926193751-448758571-1187696956=:25684--