From: "Wolfgang Nádasi-Donner" Date: 2007-05-12T04:50:04+09:00 Subject: Re: how to remove dups from 2 lists? > 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 when 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] == 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 = %w{a b c d e f g a b c} => ["a", "b", "c", "d", "e", "f", "g", "a", "b", "c"] irb(main):002:0> b = %w{e f g h i j k h i j} => ["e", "f", "g", "h", "i", "j", "k", "h", "i", "j"] irb(main):003:0> a - (a & b) => ["a", "b", "c", "d", "a", "b", "c"] irb(main):004:0> b - (a & b) => ["h", "i", "j", "k", "h", "i", "j"] Wolfgang N�dasi-Donner