0

Looking for the most efficient way. I found this on comparing lists regardless of order: https://answers.unity.com/questions/1307074/how-do-i-compare-two-lists-for-equality-not-caring.html

What about comparing array contents regardless of order?

UserDude
  • 323
  • 1
  • 3
  • 18
  • Just change `List` to `IEnumerable` and `foreach` instead of `for` in the body of method – JohanP Mar 18 '19 at 05:34
  • 1
    Possible duplicate of [Is there a built-in method to compare collections?](https://stackoverflow.com/a/12470106/34092) – mjwills Mar 18 '19 at 05:37
  • If this answer here https://stackoverflow.com/questions/2799427/what-guarantees-are-there-on-the-run-time-complexity-big-o-of-linq-methods can be trusted, then arrayA.Except(arrayB) should give you the difference between two arrays at O(n) or near that. So if the difference has a count of zero then they are equal (assuming their lengths are the same). – Reasurria Mar 18 '19 at 05:45
  • `Except` removes duplicates. So `{1,1,1,2,3,3}` will be the 'same' as `{1,1,2,2,2,3}` if you are using `Except`. – mjwills Mar 18 '19 at 05:57
  • @mjwills You're right. That was a horrible suggestion. – Reasurria Mar 18 '19 at 06:15

1 Answers1

4

You can use the Intersect method. Here is a simple console application

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        var nums1 = new int[] { 2, 4, 6, 8, 10, 9 };
        var nums2 = new int[] { 1, 3, 6, 9, 12, 2 };

        if (nums1.Intersect(nums2).Any()) // check if there is equal items
        {
            var equalItems = nums1.Intersect(nums2); // get list of equal items (2, 6, 9)

            // ...
        }
    }
}
Masoud Darvishian
  • 3,754
  • 4
  • 33
  • 41