Removing elements in a list that are also in another list

A

Adam Hartshorne

Hi All,

I was wondering if somebody could tell me if there is an efficient way
to do the following. Say I have a list(or vector) A and a list B, I want
to remove any elements in B, that are also elements in A.

Adam
 
V

Victor Bazarov

Adam said:
I was wondering if somebody could tell me if there is an efficient way
to do the following. Say I have a list(or vector) A and a list B, I want
to remove any elements in B, that are also elements in A.

Sort both lists and use 'set_difference'.

V
 
P

Patrick Kowalzick

Hello Victor,
Sort both lists and use 'set_difference'.

For set_difference you have to create a target list/vector. Do you know a
solution to remove the elements in B directly?

Regards,
Patrick
 
P

Pete Becker

Patrick said:
Hello Victor,




For set_difference you have to create a target list/vector. Do you know a
solution to remove the elements in B directly?

Assuming you've already sorted the two lists, you do essentially what
set_difference does: start with the first element from the selector
list, and walk through the target list until you reach an element that
isn't less than that one. If it's equal, remove it and move to the next
element. Move to the next element in the selector list. Repeat until done.
 
V

Victor Bazarov

Patrick said:
For set_difference you have to create a target list/vector. Do you know a
solution to remove the elements in B directly?

A solution would be to write your own "is_contained_in" predicate and then
run 'remove_if'. Something like

list_A.remove_if(is_contained_in(list_B));

That has O(n*m) complexity but without them both sorted it should suffice.

V
 
P

Patrick Kowalzick

Hello Victor,
A solution would be to write your own "is_contained_in" predicate and then
run 'remove_if'. Something like

list_A.remove_if(is_contained_in(list_B));

That has O(n*m) complexity but without them both sorted it should suffice.

Yes, this would be a solution for unsorted lists. For sorted lists it could
be more effective.

Thanks,
Patrick
 
P

Patrick Kowalzick

Hello Pete,
Assuming you've already sorted the two lists, you do essentially what
set_difference does: start with the first element from the selector list,
and walk through the target list until you reach an element that isn't
less than that one. If it's equal, remove it and move to the next element.
Move to the next element in the selector list. Repeat until done.

"remove" in the sense of the Standard Library (pushing non removed elements
as far to the front as possible?).

So we get something like:
iterator remove_set_difference( iterator,
iterator,const_iterator,const_iterator );
v.erase( v.remove_set_difference( v.begin(), v.end(), v2.begin(),
v1.begin() ), v.end() );

Regards,
Patrick
 
P

Pete Becker

Patrick said:
Hello Pete,




"remove" in the sense of the Standard Library (pushing non removed elements
as far to the front as possible?).

No, remove as in remove from the list. We're dealing here with a
container, so we're not limited to the thngs you can do with a sequence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top