473,508 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Merge lists

Tem
List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);

List<intb = new List<int>();
b.Add(3);
b.Add(4);
b.Add(5);
What's the best way to merge 2 lists with no duplicate elements? the merged
list would have 5 elements 1,2,3,4,5

Jun 27 '08 #1
9 8196
On Sun, 27 Apr 2008 17:00:51 -0700, Tem <te*****@yahoo.comwrote:
[...]
What's the best way to merge 2 lists with no duplicate elements? the
merged list would have 5 elements 1,2,3,4,5
Define "best way" and "merged".

Is the input already sorted (as in your example)? Then a merge sort would
work well. Google can help you find details on the algorithm.

If the data's not already sorted, it might make more sense to use a
dictionary, to which you add every element of one list, and then iterate
the other list, adding to the dictionary only those elements that aren't
already in the dictionary. Then all of the values in the dictionary would
be your final collection.

If the lists are especially large and memory capacity is likely to be an
issue, then it may be better to just sort the lists and do the merge sort
solution.

It's really hard to say for sure what approach is literally the "best"
with only the information you've provided here.

Pete
Jun 27 '08 #2
Tem
Best = easiest/most efficient

b could be merged into a or to a new int List
List<intc

it has to be the same type so can't use a dictionary
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Sun, 27 Apr 2008 17:00:51 -0700, Tem <te*****@yahoo.comwrote:
>[...]
What's the best way to merge 2 lists with no duplicate elements? the
merged list would have 5 elements 1,2,3,4,5

Define "best way" and "merged".

Is the input already sorted (as in your example)? Then a merge sort would
work well. Google can help you find details on the algorithm.

If the data's not already sorted, it might make more sense to use a
dictionary, to which you add every element of one list, and then iterate
the other list, adding to the dictionary only those elements that aren't
already in the dictionary. Then all of the values in the dictionary would
be your final collection.

If the lists are especially large and memory capacity is likely to be an
issue, then it may be better to just sort the lists and do the merge sort
solution.

It's really hard to say for sure what approach is literally the "best"
with only the information you've provided here.

Pete
Jun 27 '08 #3
Tem
Best = easiest/most efficient

b could be merged into a or to a new int List
List<intc

it has to be the same type so can't use a dictionary
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Sun, 27 Apr 2008 17:00:51 -0700, Tem <te*****@yahoo.comwrote:
>[...]
What's the best way to merge 2 lists with no duplicate elements? the
merged list would have 5 elements 1,2,3,4,5

Define "best way" and "merged".

Is the input already sorted (as in your example)? Then a merge sort would
work well. Google can help you find details on the algorithm.

If the data's not already sorted, it might make more sense to use a
dictionary, to which you add every element of one list, and then iterate
the other list, adding to the dictionary only those elements that aren't
already in the dictionary. Then all of the values in the dictionary would
be your final collection.

If the lists are especially large and memory capacity is likely to be an
issue, then it may be better to just sort the lists and do the merge sort
solution.

It's really hard to say for sure what approach is literally the "best"
with only the information you've provided here.

Pete
Jun 27 '08 #4
"it has to be the same type so can't use a dictionary".....
Isn't a generic Dictionary strongly typed when you instantiate it? This
doesn't make sense.
-- Peter
To be a success, arm yourself with the tools you need and learn how to use
them.

Site: http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://ittyurl.net
Jun 27 '08 #5
On Sun, 27 Apr 2008 17:56:56 -0700, Tem <te*****@yahoo.comwrote:
Best = easiest/most efficient
That's not an answer. What's easiest is not always what's most efficient.
b could be merged into a or to a new int List
List<intc
You still haven't answered my previous question about the exact nature of
the input.
it has to be the same type so can't use a dictionary
I assume you mean the result has to be a List<T>. This is irrelevant; you
can always use a Dictionary<as the intermediate type and then create a
final new List<Tfrom the result.

Pete
Jun 27 '08 #6
On Sun, 27 Apr 2008 17:00:51 -0700, "Tem" <te*****@yahoo.comwrote:
>List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);

List<intb = new List<int>();
b.Add(3);
b.Add(4);
b.Add(5);
What's the best way to merge 2 lists with no duplicate elements? the merged
list would have 5 elements 1,2,3,4,5
As others have stated, you haven't provided enough details on what can
be assumed about the input lists, and the requirements for the
resulting list, to suggest an optimal (and correct) solution. If you
target .NET 3.5 though, you may want to check if this fits the bill:

List<intc = Enumerable.Union(a, b).ToList();

Regards,
Gilles.

Jun 27 '08 #7
On Mon, 28 Apr 2008 05:16:00 -0700, SMJT <sh*************@hotmail.com
wrote:
Any reason why you can't just merge as follows;

List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);
List<intb = new List<int>();
b.Add(3);
b.Add(4);
b.Add(5);

List<intc = new List<int>();
foreach (var item in a)
{
if (!c.Contains(item))
This if() is superfluous. Tem has said that the original list elements
are unique, so no need to check for duplicates copying the first list.
c.Add(item);
}
foreach (int item in b)
{
if (!c.Contains(item))
Instead, it would be better to use "a.Contains()".

Using "c.Contains()" means the list being inspected gets longer and longer
with each addition. For very short lists, this won't matter much, but for
any non-trivial list it could mean a significant difference in execution
speed. Again, since the list elements are known to be unique, it's only
necessary to check against the other list, not the currently generated
list, and since the other list is always going to be shorter than the
currently generated list, that's a better choice.

Of course, I suppose one could argue that since this solution is pretty
much the least efficient approach to the problem, that fixing the if()
statements as noted above won't make much difference. In some respect,
that'd be true. But even if one is doing the least efficient solution, I
see no reason to make it even _less_ efficient than it nominally has to
be. :)
c.Add(item);
}
Tem: other than the comments above, I would say that this solution is the
easiest. Since you haven't been more specific about your requirement of
"best", and since "easiest" was one of the conditions you consider to
qualify for "best", I'd say that the above meets your criteria.

Again, "easiest" and "most efficient" are not always the same. The above
is a good example of this. The above would be among the least efficient
solutions.

But if your lists are short and/or ease of coding is more important that
performance, I don't see any reason to do anything more complicated.

Pete
Jun 27 '08 #8
Tem
I got it to work. thank you. I will choose my words more carefully next
time.

Tem

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 28 Apr 2008 05:16:00 -0700, SMJT <sh*************@hotmail.com>
wrote:
Any reason why you can't just merge as follows;

List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);
List<intb = new List<int>();
b.Add(3);
b.Add(4);
b.Add(5);

List<intc = new List<int>();
foreach (var item in a)
{
if (!c.Contains(item))
This if() is superfluous. Tem has said that the original list elements
are unique, so no need to check for duplicates copying the first list.
c.Add(item);
}
foreach (int item in b)
{
if (!c.Contains(item))
Instead, it would be better to use "a.Contains()".

Using "c.Contains()" means the list being inspected gets longer and longer
with each addition. For very short lists, this won't matter much, but for
any non-trivial list it could mean a significant difference in execution
speed. Again, since the list elements are known to be unique, it's only
necessary to check against the other list, not the currently generated
list, and since the other list is always going to be shorter than the
currently generated list, that's a better choice.

Of course, I suppose one could argue that since this solution is pretty
much the least efficient approach to the problem, that fixing the if()
statements as noted above won't make much difference. In some respect,
that'd be true. But even if one is doing the least efficient solution, I
see no reason to make it even _less_ efficient than it nominally has to
be. :)
c.Add(item);
}
Tem: other than the comments above, I would say that this solution is the
easiest. Since you haven't been more specific about your requirement of
"best", and since "easiest" was one of the conditions you consider to
qualify for "best", I'd say that the above meets your criteria.

Again, "easiest" and "most efficient" are not always the same. The above
is a good example of this. The above would be among the least efficient
solutions.

But if your lists are short and/or ease of coding is more important that
performance, I don't see any reason to do anything more complicated.

Pete

Jun 27 '08 #9
Tem wrote:
List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);

List<intb = new List<int>();
b.Add(3);
b.Add(4);
b.Add(5);
What's the best way to merge 2 lists with no duplicate elements? the
merged list would have 5 elements 1,2,3,4,5
..NET 3.5 has a new HashSet<Ttype that can be used for this:
--- cut here ---
using System;
using System.Collections.Generic;

namespace ShortAndComplete
{
class Program
{
static void Main(String[] args)
{
HashSet<Int32hs1 = new HashSet<Int32>(new Int32[] { 1, 2,
3 });
HashSet<Int32hs2 = new HashSet<Int32>(new Int32[] { 3, 4,
5 });
hs1.UnionWith(hs2);
foreach (Int32 value in hs1)
Console.Out.WriteLine(value);
Console.ReadLine();
}
}
}
--- cut here ---

If you're not using .NET 3.5, either use Dictionary as suggested by
others, or you can download and use my Set<Tclass for .NET 2.0:

https://vkarlsen.serveftp.com:8443/s...ections/Set.cs

username and password is both 'guest' without the quotes.

Note that by itself the Set class needs some resources for its exception
messages, but you can safely just replace those with something suitable,
if you decide to use it.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #10

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

Similar topics

5
4488
by: Alan Little | last post by:
I need to merge and de-duplicate some lists, and I have some code which works but doesn't seem particularly elegant. I was wondering if somebody could point me at a cleaner way to do it. Here's...
10
2157
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
1
12825
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
7
2434
by: deanfamily | last post by:
Here's the problem: I have two linked lists of integers (list1 and list2) that are already in sequential order, and I need to combine them into one list in sequential order (newList). Any...
9
3017
by: anunaygupta | last post by:
Hello all, I have a data structures problem. Assume there are 2 linked lists L1 and L2. They merge into some node. L1 1->2->3->4->5->6 / L2 8->7->9->/ L1 and L2 merge in node with value...
51
8566
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
6
10361
by: Tekkaman | last post by:
I have a list of lists and I want to define an iterator (let's call that uniter) over all unique elements, in any order. For example, calling: sorted(uniter(, , ])) must return . I tried the...
9
9019
by: Aaron Watters | last post by:
....is to forget they are sorted??? While trying to optimize some NUCULAR libraries I discovered that the best way to merge 2 sorted lists together into a new sorted list is to just append them...
14
3991
by: etal | last post by:
Here's an algorithm question: How should I efficiently merge a collection of mostly similar lists, with different lengths and arbitrary contents, while eliminating duplicates and preserving order...
0
7115
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...
0
7377
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...
1
7036
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...
0
7489
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...
0
5624
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,...
0
3191
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...
0
1547
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 ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
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...

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.