Here we want to compare whether the content in the two arrays is the same. Take the int array as an example.
Int [] Arraya = new [] {1, 2, 3, 4, 5}
Int [] Arrayb = new [] {5, 3, 2, 1, 4}
The values in the preceding two arrays are the same, all of which are 1, 2, 3, 4, 5. The specific algorithm is as follows.
The first method is the original method, which uses loops.
Public static bool compareArr (int [] arr1, int [] arr2 ){
Bool [] flag = new bool [arr1.Length]; // initialize a bool array. The initial value is false;
For (int I = 0; I <arr1.Length; I ++ ){
For (int j = 0; j <arr2.Length; j ++ ){
If (arr1 [I] = arr2 [j]) flag [I] = true; // if the same value exists, set the value of the corresponding bool array to true;
}
}
Foreach (var item in flag ){
If (item = false) return false; // if the bool array is traversed and the value is false, different values are returned.
}
Return true;
}
The second method can be compared by using Linq query.
{
}