Merge lists

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tem

    Merge lists

    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

  • Peter Duniho

    #2
    Re: Merge lists

    On Sun, 27 Apr 2008 17:00:51 -0700, Tem <tem1232@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

    Comment

    • Tem

      #3
      Re: Merge lists

      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" <NpOeStPeAdM@nn owslpianmk.comw rote in message
      news:op.uaaojbo 98jd0ej@petes-computer.local. ..
      On Sun, 27 Apr 2008 17:00:51 -0700, Tem <tem1232@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

      Comment

      • Tem

        #4
        Re: Merge lists

        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" <NpOeStPeAdM@nn owslpianmk.comw rote in message
        news:op.uaaojbo 98jd0ej@petes-computer.local. ..
        On Sun, 27 Apr 2008 17:00:51 -0700, Tem <tem1232@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

        Comment

        • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

          #5
          Re: Merge lists

          "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




          Comment

          • Peter Duniho

            #6
            Re: Merge lists

            On Sun, 27 Apr 2008 17:56:56 -0700, Tem <tem1232@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

            Comment

            • Gilles Kohl [MVP]

              #7
              Re: Merge lists

              On Sun, 27 Apr 2008 17:00:51 -0700, "Tem" <tem1232@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.Unio n(a, b).ToList();

              Regards,
              Gilles.

              Comment

              • Peter Duniho

                #8
                Re: Merge lists

                On Mon, 28 Apr 2008 05:16:00 -0700, SMJT <shanemjtownsen d@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(it em))
                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(it em))
                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

                Comment

                • Tem

                  #9
                  Re: Merge lists

                  I got it to work. thank you. I will choose my words more carefully next
                  time.

                  Tem

                  "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
                  news:op.uabywvs m8jd0ej@petes-computer.local. ..
                  On Mon, 28 Apr 2008 05:16:00 -0700, SMJT <shanemjtownsen d@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(it em))
                  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(it em))
                  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

                  Comment

                  • =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

                    #10
                    Re: Merge lists

                    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.Collecti ons.Generic;

                    namespace ShortAndComplet e
                    {
                    class Program
                    {
                    static void Main(String[] args)
                    {
                    HashSet<Int32hs 1 = new HashSet<Int32>( new Int32[] { 1, 2,
                    3 });
                    HashSet<Int32hs 2 = new HashSet<Int32>( new Int32[] { 3, 4,
                    5 });
                    hs1.UnionWith(h s2);
                    foreach (Int32 value in hs1)
                    Console.Out.Wri teLine(value);
                    Console.ReadLin e();
                    }
                    }
                    }
                    --- 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:



                    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:lasse@vk arlsen.no
                    Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

                    PGP KeyID: 0xBCDEA2E3

                    Comment

                    Working...