I am trying to check if an Array A
contains all the elements of Array B
and similarly Array B
contains elements of Array A
, the order does not matter.
Some examples:
A B Result {1} {0} false {0, 0, 0} {0, 0, 0} true {17, 4, 11} {4, 17, 11} true
I have written the following code:
public static bool CheckEquality(int[] a, int[] b)
{
return Array.TrueForAll(a, m => b.Count(z => m == z) == a.Count(z => m == z)) && a.Length == b.Length;
}
Both arrays contain just integers. Both arrays should strictly contain the same number of the same elements, and their lengths should be equal.
Is there a better and efficient way to do this?
{ 0, 0, 1, 1 }
and{ 0, 1, 1, 1 }
? (assuming it should return false, as your current code seem to already do) \$\endgroup\$