Whether the array comparison is the same

Source: Internet
Author: User

Environment: array A and array B


Int [] ArrayA = new [] {1, 2, 3, 4, 5, 6, 7, 8, 9 };
Int [] ArrayB = new [] {9, 1, 4, 5, 2, 3, 6, 7, 8 };
We need to compare the content of the Array instead of the object reference of the Array. Therefore, we cannot take Array. Equals (ArrayA, ArrayB) (the result is false)

Now let's take a look at how to compare the array content. Of course you can compare it with loops. Here we will not talk about the method of loop comparison. Now let's look at other methods:

Method 1: Use the generic Method

Static bool ArraysEqual <T> (T [] a1, T [] a2) {if (ReferenceEquals (a1, a2) return true; if (a1 = null | a2 = null) return false; if (a1.Length! = A2.Length) return false; EqualityComparer <T> comparer = EqualityComparer <T>. Default; for (int I = 0; I <a1.Length; I ++) {if (! Comparer. Equals (a1 [I], a2 [I]) return false;} return true;} [html] view plaincopyprint? Static bool ArraysEqual <T> (T [] a1, T [] a2) {if (ReferenceEquals (a1, a2) return true; if (a1 = null | a2 = null) return false; if (a1.Length! = A2.Length) return false; EqualityComparer <T> comparer = EqualityComparer <T>. Default; for (int I = 0; I <a1.Length; I ++) {if (! Comparer. Equals (a1 [I], a2 [I]) return false;} return true;} static bool ArraysEqual <T> (T [] a1, T [] a2)
{
If (ReferenceEquals (a1, a2 ))
Return true;

If (a1 = null | a2 = null)
Return false;

If (a1.Length! = A2.Length)
Return false;

EqualityComparer <T> comparer = EqualityComparer <T>. Default;
For (int I = 0; I <a1.Length; I ++)
{
If (! Comparer. Equals (a1 [I], a2 [I]) return false;
}
Return true;
}


Method 2: Extended method of Enumerable. SequenceEqual: Determine whether the two sequences are equal. For more information, see msdn: http://msdn.microsoft.com/en-us/library/bb348567.aspx.

Bool equals = ArrayA. OrderBy (
=> A). SequenceEqual (ArrayB. OrderBy (a => ));

 

 

Method 3: Use the Linq method to query the number of data comparisons of the intersection.

Var q = from a in ArrayA join B in ArrayB on a equals B select a; bool limit S1 = ArrayA. length = ArrayB. length & q. count () = ArrayA. length;
[Html] view plaincopyprint? Var q = from a in ArrayA join B in ArrayB on a equals B select a; bool limit S1 = ArrayA. length = ArrayB. length & q. count () = ArrayA. length; var q = from a in ArrayA
Join B in ArrayB on a equals B
Select;


Bool defaults S1 = ArrayA. Length = ArrayB. Length & q. Count () = ArrayA. Length;


Method 4: ALL
Var set = new HashSet <byte> (ArrayA );
Bool allThere = ArrayB. All (set. Contains );

 

Method 5: use mathematical ideas to use intersection and Union ideas
Bool upload S3 = ArrayA. Intersect (ArrayB). Count () = ArrayA. Union (ArrayB). Count ();


 

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.