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