Compare two arrays using C#

obautista
obautista used Ask the Experts™
on
What is the best way to compare or find matches in two arrays or collections (i.e. Lists)?  If an example code snippet could be provided - that would be great.

Thanks -
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sr. System Analyst
Commented:
List a,b,c (pseudo code)

a = (...) // set a here
b = (...) // set b here
c = () // empty

for each (ia in a)
 for each (ib in b)
    if (ai=bi) then c.add (ai)
         bool equal = (arr1.Length == arr2.Length);
           
            if (equal)
            {
                int i = arr1.Length;
                int j = 0;
                while ((j < i) && (equal == true))
                {
                    equal = arr1[j] == arr2[j];
                    if (equal == false)
                        break;
                    j++;
                }
            }

           
Commented:
If you don't want to loop through the two arrays, and if you are familiar with C++, you can get the address of the two arrays and there buffer size. Compare the two buffers directly will give you a match, or not. You can do it in your C# codes with a unsafe block.

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial