Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to compare two int arrays of the same length?

what is the best way to compare int arrays b and c with a:

int a[] = {0,1,0,0,1};
int b[] = {0,1,0,0,1};
int c[] = {1,1,0,0,1};

b and c are just examples, assume they can be any combination of 0s and 1s.

I am trying to detect arrays identical to a. I have googled this for a while and have not found a satisfactory answer.

This is a beginners question I realise, thank you for your patience.

like image 509
user1083734 Avatar asked Feb 02 '12 21:02

user1083734


People also ask

How to efficiently compare two integer arrays with JavaScript?

To efficiently compare two integer arrays and find the items in both arrays with JavaScript, we can use some JavaScript array methods. to call a1.filter with a callback that checks if a1 array entry a is in a2 with a2.includes. Therefore, inBoth is [15, 25, 11].

How do you compare two arrays in Python?

Arrays.compare (array1,array2); // array1 and array2 are two arrays Parameters and Return Type: The method compare () accepts an array as parameters with different data types example: string, integer, float, double, long, etc. The return type of this method is an integer.

How to deep compare two arrays in Java?

Actually, there is a list of equals () methods in Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is base of all classes in Java). How to Deep compare array contents? As seen above, the Arrays.equals () works fine and compares arrays contents.

How to compare two arrays lexicographically in Java?

Arrays compare () method in Java comes under the Arrays class and java.util package. This method compares two arrays lexicographically (Dictionary order). There are two different versions of different overloads for Boolean, byte, char, double, float, int, long, short, and Object arrays. This method returns values as per the below-mentioned cases.


1 Answers

Use the standard memcmp function from <string.h>.

memcmp(a, b, sizeof(a)) == 0

whenever a and b are equal.

like image 166
Fred Foo Avatar answered Nov 07 '22 04:11

Fred Foo