473,788 Members | 3,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Removing elements in a list that are also in another list

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
Jan 26 '06 #1
7 2103
Adam Hartshorne wrote:
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
Jan 26 '06 #2
Hello Victor,
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 '.


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
Jan 27 '06 #3
Patrick Kowalzick wrote:
Hello Victor,

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 '.

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jan 27 '06 #4
Patrick Kowalzick wrote:
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 '.

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_i n" predicate and then
run 'remove_if'. Something like

list_A.remove_i f(is_contained_ in(list_B));

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

V
Jan 27 '06 #5
Hello Victor,
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 '.

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_i n" predicate and then
run 'remove_if'. Something like

list_A.remove_i f(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
Jan 27 '06 #6
Hello Pete,
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 '.

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.


"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_diff erence( iterator,
iterator,const_ iterator,const_ iterator );
v.erase( v.remove_set_di fference( v.begin(), v.end(), v2.begin(),
v1.begin() ), v.end() );

Regards,
Patrick
Jan 27 '06 #7
Patrick Kowalzick wrote:
Hello Pete,

>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 '.
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_differenc e 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?).


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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jan 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2730
by: Amit | last post by:
Hi, I have a list of integers. At each iteration, I remove some element from it and then insert new elements in it. The order of elements is not important. So I guess I could use a vector also for this purpose. However, I am also interested in having no duplicacy in elements of the vector. So I do not want to have any integer repeated more than once in the list. One very naive approach could be to compare the new integer being added to...
2
4742
by: vsgdp | last post by:
From what I learned, if you want to do random element insertions and deletions you should use a list. But, with std::vector, if the order of the elements does not matter, couldn't you efficiently remove a random element by swapping it with the last element and then just using pop_back? Does erase do this internally? Or does erase do the element shift to fill in the gap?
24
4403
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what we covered was within the last week of the class and all self taught. Our prof gave us an example of a Java method used to remove elements from an array: public void searchProcess() { int outIt=0;
2
1712
by: Adam Hartshorne | last post by:
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
5
2016
by: nuffnough | last post by:
This is python 2.4.3 on WinXP under PythonWin. I have a config file with many blank lines and many other lines that I don't need. read the file in, splitlines to make a list, then run a loop that looks like this:
6
6116
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or more minutes. 'REMOVE DUBLICATED VALUE FROM ARRAY +++++++++++++++++ Dim col As New Scripting.Dictionary Dim ii As Integer = 0
10
6077
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to remove the elements with odd values from your list * and the even values from your vector.
7
2845
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds to support cloning: Public Class RefClass Public tcp As TcpClient Public name As String End Class Public Class RefClassList Inherits List(Of RefClass) Implements ICloneable
3
11592
m6s
by: m6s | last post by:
Hello to all, I am having trouble removing an item from list, which the item is a list by itself. <code> for self.line in self.filtered: if self.appid: if self.line <> self.appid: print RED + " ".join( map ( lambda x:str(x), self.filtered )) + RESET self.filtered.remove( self.line.index(self.line ) ) else: print "F: " + str(self.line)
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.