Robert Klemme
5/11/2007 7:07:00 AM
On 11.05.2007 06:46, Alex Gutteridge wrote:
> On 11 May 2007, at 11:18, Mike Steiner wrote:
>
>> 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
>
> Here is how I would write it: (it didn't give quite the same results as
> your method but seems to be closer to what you asked for so...):
>
> def remove_duplicates(list1,list2)
> (list1 & list2).each do |x|
> list1.delete(x)
> list2.delete(x)
> end
> return list1,list2
> end
>
> irb(main):028:0> RemoveDupsFromLists([1,1,2,3,4],[2,2,3,5,5])
> => [[1, 1, 4], [2, 5, 5]]
> irb(main):029:0> remove_duplicates([1,1,2,3,4],[2,2,3,5,5])
> => [[1, 1, 4], [5, 5]]
>
> Be careful with your method since it alters the Arrays as you iterate
> through them and will give hard to understand results sometimes. In the
> example above '2' is a duplicate but is only deleted from the second
> array once because of this problem/feature.
Sets also come in handy - especially if those lists are large.
Kind regards
robert