compare a list of integers

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

    compare a list of integers

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

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

    I need to compare two integer lists.
    It returns true if values and ordering are the same.
    Is there a better way to do this than a foreach loop?

    Tem
  • Jon Skeet [C# MVP]

    #2
    Re: compare a list of integers

    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(1);
    b.Add(2);
    b.Add(3);
    >
    I need to compare two integer lists.
    It returns true if values and ordering are the same.
    Is there a better way to do this than a foreach loop?
    Which version of .NET are you using? .NET 3.5 has an extension method
    on IEnumerable<Tof SequenceEqual:

    if (a.SequenceEqua l(b))
    ....

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    World class .NET training in the UK: http://iterativetraining.co.uk

    Comment

    • Tem

      #3
      Re: compare a list of integers

      3.5
      but I thought extension methods would make things slow? been trying to avoid
      them


      "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
      news:MPG.227e4f bd1f62c6a5c8d@m snews.microsoft .com...
      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(1);
      >b.Add(2);
      >b.Add(3);
      >>
      >I need to compare two integer lists.
      >It returns true if values and ordering are the same.
      >Is there a better way to do this than a foreach loop?
      >
      Which version of .NET are you using? .NET 3.5 has an extension method
      on IEnumerable<Tof SequenceEqual:
      >
      if (a.SequenceEqua l(b))
      ...
      >
      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      World class .NET training in the UK: http://iterativetraining.co.uk

      Comment

      • Marc Gravell

        #4
        Re: compare a list of integers

        but I thought extension methods would make things slow? been trying to avoid
        them
        Why would you think that? OK, in reality you could shave a few cycles
        if you know you have List<Tby checking the length first
        (SequenceEqual doesn't do this, although this could be a missed
        opportunity - it could check if both operands implement IList, and
        compare the length) - and because it is using the interface (rather
        than direct to the sealed List<T>) it won't inline any of the fetches
        - but this is rarely the most significant thing.

        My advice: use the simplest code that clearly expresses what you want
        to do. SequenceEqual does this; *if* you later find this is a
        bottleneck, then tweak it (perhaps introduce a List<TSequenceE qual
        extension method) - but until then you're optimising prematurely.

        Marc

        Comment

        • Marc Gravell

          #5
          Re: compare a list of integers

          For reference - an additional overload that checks a few obvious cases
          first... just add your namespace as a "using" at the top, and the
          overload resolution will select it instead of
          Enumerable.Sequ enceEqual, since ICollection<Tis more specific than
          IEnumerable<T>

          Adding a List<T>-specific version would almost certainly be overkill;
          I doubt you'd ever see any tangible performance benefit in most code.

          Marc

          namespace SomeNamespace
          {
          public static class CollectionExt
          {
          public static bool SequenceEqual<T >(this ICollection<Tfi rst,
          ICollection<Tse cond)
          {
          return SequenceEqual<T >(first, second,
          EqualityCompare r<T>.Default);
          }
          public static bool SequenceEqual<T >(this ICollection<Tfi rst,
          ICollection<Tse cond, IEqualityCompar er<Tcomparer)
          {
          if (first == null) throw new
          ArgumentNullExc eption("first") ;
          if (second == null) throw new
          ArgumentNullExc eption("second" );
          if (ReferenceEqual s(first, second)) return true;
          if (first.Count != second.Count) return false;
          return System.Linq.Enu merable.Sequenc eEqual<T>(first ,
          second, comparer);
          }
          }
          }

          Comment

          • Tem

            #6
            Re: compare a list of integers

            Thanks Marc

            Something tells me this will be much more efficient.

            "Marc Gravell" <marc.gravell@g mail.comwrote in message
            news:98ec5ecf-e8cc-48fc-9429-aeb8e7a580ad@24 g2000hsh.google groups.com...
            For reference - an additional overload that checks a few obvious cases
            first... just add your namespace as a "using" at the top, and the
            overload resolution will select it instead of
            Enumerable.Sequ enceEqual, since ICollection<Tis more specific than
            IEnumerable<T>
            >
            Adding a List<T>-specific version would almost certainly be overkill;
            I doubt you'd ever see any tangible performance benefit in most code.
            >
            Marc
            >
            namespace SomeNamespace
            {
            public static class CollectionExt
            {
            public static bool SequenceEqual<T >(this ICollection<Tfi rst,
            ICollection<Tse cond)
            {
            return SequenceEqual<T >(first, second,
            EqualityCompare r<T>.Default);
            }
            public static bool SequenceEqual<T >(this ICollection<Tfi rst,
            ICollection<Tse cond, IEqualityCompar er<Tcomparer)
            {
            if (first == null) throw new
            ArgumentNullExc eption("first") ;
            if (second == null) throw new
            ArgumentNullExc eption("second" );
            if (ReferenceEqual s(first, second)) return true;
            if (first.Count != second.Count) return false;
            return System.Linq.Enu merable.Sequenc eEqual<T>(first ,
            second, comparer);
            }
            }
            }

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: compare a list of integers

              Tem <tem1232@yahoo. comwrote:
              Something tells me this will be much more efficient.
              Certainly in the special cases, where the two lists are either
              "obviously" different or "obviously" the same.

              But note that it's still an extension method - your earlier comment
              about how you've been "trying to avoid [extension methods]" should be
              reconsidered, IMO.

              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              World class .NET training in the UK: http://iterativetraining.co.uk

              Comment

              Working...