I have two sorted lists of positive integers which can have repeated elements and I must remove matching pairs of numbers, one from each list:
a=[1,2,2,2,3]
b=[2,3,4,5,5]
should become:
a=[1,2,2]
b=[4,5,5]
That is, the 2’s and the 3’s have been removed because they appear in both lists.
Set intersection can’t be used here because of the repeated elements.
How do I go about this?
To remove elements appearing in both lists, use the following:
To create a function which does it for you, simply do:
Or to return new lists and not to edit the old ones:
However the former could be replaced with
removeCommonElementslike so:Which would keep a and b, but create a duplicates without common elements.