From: Mike Steiner Date: 2007-05-12T08:11:37+09:00 Subject: Re: how to remove dups from 2 lists? ------=_Part_85694_33098822.1178925096495 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Good idea about the & operator for sets, but there could be more than 1 group of matches, so I'd have to iterate: a =3D [ 1, 1, 2, 3, 4, 4, 4, 5 ] b =3D [ 1, 2, 2, 4, 4, 6, 7 ] results =3D a & b while results results.each do | m | a =3D a - m b =3D b - m end results =3D a & b end I think this will work, and it's sort of elegant (well, not as elegant as having it built into Ruby already :-)). Mike Steiner On 5/11/07, Wolfgang N=E1dasi-Donner wrote: > > > On May 10, 2007, at 10:18 PM, Mike Steiner wrote: > > > >> I'm trying to write some code that removes all elements from 2 lists > that > >> are in both lists. However, I don't want any duplicates from each list > >> deleted also (which is what the array "-" operator does). The code I > have > >> now doesn't handle restarting the current iteration for both loops whe= n > a > >> match is found and deleted in both loops. Here's the code: > >> > >> def RemoveDupsFromLists ( list1 , list2 ) > >> list1.each_index do | i | > >> list2.each_index do | j | > >> if list1[i] =3D=3D list2[j] > >> list1.delete_at ( i ) > >> list2.delete_at ( j ) > >> end > >> end > >> end > >> return [ list1 , list2 ] > >> end > >> > >> What's weird is that doing this is easy in C (my first language), but > >> difficult in Ruby. Everything else I've seen has been MUCH easier in > >> Ruby. > >> > >> Mike Steiner > > Isn't this what you want? > > irb(main):001:0> a =3D %w{a b c d e f g a b c} > =3D> ["a", "b", "c", "d", "e", "f", "g", "a", "b", "c"] > irb(main):002:0> b =3D %w{e f g h i j k h i j} > =3D> ["e", "f", "g", "h", "i", "j", "k", "h", "i", "j"] > irb(main):003:0> a - (a & b) > =3D> ["a", "b", "c", "d", "a", "b", "c"] > irb(main):004:0> b - (a & b) > =3D> ["h", "i", "j", "k", "h", "i", "j"] > > Wolfgang N=E1dasi-Donner > > ------=_Part_85694_33098822.1178925096495--