C # compare the two Arrays for the same Algorithm

Source: Internet
Author: User

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.

{

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.