473,799 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

determine common members in list of lists

InstrumentPrope rtyEntity
string Name

InstrumentEntit y
string Name
List<Instrument PropertyEntityI nstrumentProper ties
For a List<Instrument Entity, I want to determine the
List<Instrument PropertyEntityh eld in common by all members.
What is the most performant algorithm ?
Jul 17 '08 #1
6 3073
On Jul 18, 3:46*am, "John A Grandy" <johnagrandy-at-gmail-dot-com>
wrote:
InstrumentPrope rtyEntity
* * string Name

InstrumentEntit y
* * string Name
* * List<Instrument PropertyEntityI nstrumentProper ties

For a List<Instrument Entity, I want to determine the
List<Instrument PropertyEntityh eld in common by all members.

What is the most performant algorithm ?
If you need it to be fast, you shouldn't use List<T>. If
InstrumentPrope rties would have been of type HashSet<T>, then you
could use its IntersectWith method in a foreach loop to efficiently
find the intersection of InstrumentPrope rties for all your elements -
and that would be what you want. For plain List<T>, you can use
Enumerable.Inte rsect, but it's not any more efficient than a plain
nested foreach.
Jul 18 '08 #2
How about transferring the contents of the first List to a HashSet and then
successively call HashSet.Interse ctWith( List ) for each remaining List ?

"Pavel Minaev" <in****@gmail.c omwrote in message
news:69******** *************** ***********@j1g 2000prb.googleg roups.com...
On Jul 18, 3:46 am, "John A Grandy" <johnagrandy-at-gmail-dot-com>
wrote:
InstrumentPrope rtyEntity
string Name

InstrumentEntit y
string Name
List<Instrument PropertyEntityI nstrumentProper ties

For a List<Instrument Entity, I want to determine the
List<Instrument PropertyEntityh eld in common by all members.

What is the most performant algorithm ?
If you need it to be fast, you shouldn't use List<T>. If
InstrumentPrope rties would have been of type HashSet<T>, then you
could use its IntersectWith method in a foreach loop to efficiently
find the intersection of InstrumentPrope rties for all your elements -
and that would be what you want. For plain List<T>, you can use
Enumerable.Inte rsect, but it's not any more efficient than a plain
nested foreach.

Jul 18 '08 #3
Just noticed that HashSet was introduced in 3.5. I am on 2.0.

"John A Grandy" <johnagrandy-at-gmail-dot-comwrote in message
news:Ol******** ******@TK2MSFTN GP02.phx.gbl...
How about transferring the contents of the first List to a HashSet and
then successively call HashSet.Interse ctWith( List ) for each remaining
List ?

"Pavel Minaev" <in****@gmail.c omwrote in message
news:69******** *************** ***********@j1g 2000prb.googleg roups.com...
On Jul 18, 3:46 am, "John A Grandy" <johnagrandy-at-gmail-dot-com>
wrote:
>InstrumentProp ertyEntity
string Name

InstrumentEnti ty
string Name
List<Instrumen tPropertyEntity InstrumentPrope rties

For a List<Instrument Entity, I want to determine the
List<Instrumen tPropertyEntity held in common by all members.

What is the most performant algorithm ?

If you need it to be fast, you shouldn't use List<T>. If
InstrumentPrope rties would have been of type HashSet<T>, then you
could use its IntersectWith method in a foreach loop to efficiently
find the intersection of InstrumentPrope rties for all your elements -
and that would be what you want. For plain List<T>, you can use
Enumerable.Inte rsect, but it's not any more efficient than a plain
nested foreach.

Jul 18 '08 #4
On Jul 18, 9:04*pm, "John A Grandy" <johnagrandy-at-gmail-dot-com>
wrote:
How about transferring the contents of the first List to a HashSet and then
successively call HashSet.Interse ctWith( List ) for each remaining List ?
InteesectWith requires the argument to be a HashSet as well, so you'll
need to convert both.
Jul 18 '08 #5
How is IntersectWith determining equality ? Is it instance equality ? Or
does it compare the values of all members of each instance ?

My instance equality is based on the Name property only.

InstrumentPrope rty1.Name == InstrumentPrope rty2.Name
"Pavel Minaev" <in****@gmail.c omwrote in message
news:8e******** *************** ***********@z72 g2000hsb.google groups.com...
On Jul 18, 9:04 pm, "John A Grandy" <johnagrandy-at-gmail-dot-com>
wrote:
How about transferring the contents of the first List to a HashSet and
then
successively call HashSet.Interse ctWith( List ) for each remaining List ?
InteesectWith requires the argument to be a HashSet as well, so you'll
need to convert both.
Jul 18 '08 #6
On Jul 18, 9:14*pm, "John A Grandy" <johnagrandy-at-gmail-dot-com>
wrote:
Just noticed that HashSet was introduced in 3.5. I am on 2.0.
You can always use Dictionary with dummy values - performance-wise,
it's the same as HashSet - but you'd still have to code Intersect
youself. Perhaps this would do the trick:

IEnumerable<TIn tersect<T>(IEnu merable<Txs, IEnumerable<Tys )
{
Dictionary<T, boolzs = new Dictionary<T, bool>();
foreach (T x in xs)
{
zs.Add(x, false);
}
foreach (T y in ys)
{
bool dummy;
if (zs.TryGetValue (y, out dummy))
{
zs[y] = true;
}
}
foreach (KeyValuePair<T , boolz in zs)
{
if (z.Value)
{
yield return z.Key;
}
}
}
Jul 18 '08 #7

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

Similar topics

3
5004
by: Arto Huusko | last post by:
Hello, I'm wondering about the portability and correctness of the following scenario. I have a generic doubly linked list structure, and it contains generic nodes that look like this: struct node {
13
7300
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free( structure->bufferspace ) ). My question is, if I free just the structure, will the (e.g.) bufferspace be freed implicitly, or do I have to (as I currently am) free the members first? Thanks. -cjl
1
1574
by: Grzegorz ¦lusarek | last post by:
Hi all. I my application i have situation when i have some lists and i must get from lists common elements. Exacly i don't know how many list I have so at the moment of creation each of one I append them to one list. So I get list wchich consist of lists of objects. And now i want to get common objects of this list. How to do it? Grzegorz
2
1568
by: mark4asp | last post by:
Q: Initialising and updating a class with only static members & database dependency I have a class with the following members: public static List<ACISACIS_List; static AssetClass() { // blah } public static ACIS Get_ACIS(string sACISCode) { // blah }
3
34604
JoeMac3313
by: JoeMac3313 | last post by:
My Assignment was to compare two lists and print out the number of elments that are same. It is supposed to look like this Week 7 Homework The number of common elements is: 3 The number of common elements is: 3 I get
7
2846
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
1421
by: Curious | last post by:
I have too similar methods in two classes: -----------------------DistributionFoo--------------------- class DistributionFoo : FooBase { private SortableBindingList<DistributionRowStatusmSelected; private void DoSomething() {
2
3278
by: antar2 | last post by:
Hello, I am a beginner in python. following program prints the second element in list of lists 4 for the first elements in list 4 that are common with the elements in list 5 list4 = ,,] list5 =
6
1743
by: Peter | last post by:
Hi I have a number of arrays of longs, from which I need to find a single array which only contains the values which appear in all the original arrays. For example, I could have the three arrays: 1, 3, 2, 8, 5 3, 6, 1
0
9546
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
10491
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...
0
10268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10031
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
9079
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
6809
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();...
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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.