Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. traversing two lists simultaneously

traversing two lists simultaneously

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pita32
    wrote on last edited by
    #1

    suppose you have two lists of the same size, i.e. same number of elements List a, List b is it possible to traverse the two lists simultaneously and compare the values? Element 1 in list a to element 1 in list b, element 2 in list a to element 2 in list b, etc. I use a Foreach when traversing lists, but am unsure of how to do a side by side comparison of list b when I am traversing list a. Thanks

    OriginalGriffO D L J 4 Replies Last reply
    0
    • P Pita32

      suppose you have two lists of the same size, i.e. same number of elements List a, List b is it possible to traverse the two lists simultaneously and compare the values? Element 1 in list a to element 1 in list b, element 2 in list a to element 2 in list b, etc. I use a Foreach when traversing lists, but am unsure of how to do a side by side comparison of list b when I am traversing list a. Thanks

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Use a for instead of a foreach:

      for (int i = 0; i < a.Length && i < b.Length; i++)
      {
      if (a[i] == b[i])
      {
      ...
      }
      }

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • P Pita32

        suppose you have two lists of the same size, i.e. same number of elements List a, List b is it possible to traverse the two lists simultaneously and compare the values? Element 1 in list a to element 1 in list b, element 2 in list a to element 2 in list b, etc. I use a Foreach when traversing lists, but am unsure of how to do a side by side comparison of list b when I am traversing list a. Thanks

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Lists work just like arrays, so you can use either an enumerator (foreach) or you can use an index value:

        for(int index = 0; index < someValue; ++index)
        {
        
            if (a\[index\] == b\[index\])
            {
                // true
            }
            else
            {
                // false
            }
        }
        

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        1 Reply Last reply
        0
        • P Pita32

          suppose you have two lists of the same size, i.e. same number of elements List a, List b is it possible to traverse the two lists simultaneously and compare the values? Element 1 in list a to element 1 in list b, element 2 in list a to element 2 in list b, etc. I use a Foreach when traversing lists, but am unsure of how to do a side by side comparison of list b when I am traversing list a. Thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          If the "lengths" of a and b are equal, you can maintain your own index:

          int i = 0;

          foreach ( int value in a ){
          bool result = (value == b[i++]);
          }

          Note the suffix (++) operator in this case.

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          1 Reply Last reply
          0
          • P Pita32

            suppose you have two lists of the same size, i.e. same number of elements List a, List b is it possible to traverse the two lists simultaneously and compare the values? Element 1 in list a to element 1 in list b, element 2 in list a to element 2 in list b, etc. I use a Foreach when traversing lists, but am unsure of how to do a side by side comparison of list b when I am traversing list a. Thanks

            J Offline
            J Offline
            James Curran
            wrote on last edited by
            #5

            You could also use the `Zip` extension method, which takes a lambda with two arguments, which is called with the corresponding elements. It yields a IEnumerable of the results of those lambda calls.

            var a = {1,2,3,4};
            var b = {2,3,4,5}
            var c = a.Zip(b, (aa, bb)=> aa \* bb).ToList();
            //  c = {2,  6, 12, 20};
            
            //  :
            
            var d = {…};
            var e = {…};
            
            if (d.Zip(e, (dd, ee)=> dd==ee).All()) 
            {   // tests if all elements are equals  (same as a.SequenceEqual(b))
            

            Truth, James

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups