# Re: how to remove dups from 2 lists?

**URL:** <https://www.ruby-forum.com/t/re-how-to-remove-dups-from-2-lists/103923>\
**Category:** Ruby\
**Created:** [May 11, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/re-how-to-remove-dups-from-2-lists/103923 "2007-05-11T18:04:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [May 11, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/re-how-to-remove-dups-from-2-lists/103923/1 "2007-05-11T18:04:49Z")

</div>

From: Mike S. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> I’m looking for a function that will eliminate all matching  
> items, like canceling terms in a fraction. If the first list  
> has 3 "a"s, and the second list has 5 "a"s, then afterwards  
> the first list should have 0 "a"s and the second list should  
> have 2 "a"s.

class Array  
def remove\_instances( \*values )  
result = dup  
values.flatten.each{ |value|  
if i = result.index( value )  
result.delete\_at( i )  
end  
}  
result  
end  
end

a1 = %w| a a a b b c |  
a2 = %w| a a a a a b |

p a1.remove\_instances( a2 )  
#=\> [“b”, “c”]  
p a2.remove\_instances( a1 )  
#=\> [“a”, “a”]
