From: Todd Benson Date: 2007-05-11T23:40:40+09:00 Subject: Re: how to remove dups from 2 lists? ------=_Part_62389_16094718.1178894438818 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 5/11/07, Todd Benson wrote: > > This is how I would do it (that is, if I understand the OP's request). > > a = %w( a a b b c e f) > b = %w( b c c c d e e g) > > ha = {}; hb = {}; dif = {} > ha.default = hb.default = 0 > ca = []; cb = [] > > a.uniq.each { |i| a.each { |j| ha[i] += 1 if i == j } } > b.uniq.each { |i| b.each { |j| hb[j| += 1 if i == j } } > (ha.merge hb).keys.each { |k| dif[k] = ha[k] - hb[k] } > dif.each { |k,v| v>0 ? ca += (k*v).split(//) : cb += (k*-v).split(//) } > > puts "list1: #{ (dif.each { |k,v| (k*v).split(//) if v>=0 }" # ["a", "a", > "b", "f"] > puts "list2: #{ (dif.each { |k,v| (k*-v).split(//) if v<0 }" # ["c", "c", > "d", "e", "g"] > > > Of course, you can only compare 2 lists. > > Todd Sorry, the puts lines should read: puts "list1: #{ca.inspect}" puts "list2: #{cb.inspect}" ------=_Part_62389_16094718.1178894438818--