check if 2 arrays are equal code example
Example 1: check if 2 arrays are equal javascript
const a = [1, 2, 3];
const b = [4, 5, 6];
const c = [1, 2, 3];
function arrayEquals(a, b) {
return Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index]);
}
arrayEquals(a, b);
arrayEquals(a, c);
Example 2: check if 2 arrays are equal javascript
var arraysMatch = function (arr1, arr2) {
if (arr1.length !== arr2.length) return false;
for (var i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) return false;
}
return true;
};
Example 3: example to check two integer array are equal in java?
import java.util.Arrays;
public class JavaArrayConceptsConitnue
{
public static void main(String[] args)
{
int[] x = {10,12,20,30,25};
int[] subx = new int[]{10,12,20,30,26};
if(Arrays.equals(x, subx) == true)
{
System.out.println("Both the arrays are equal");
}
else
{
System.out.println("Arrays are not equal");
}
}
}
Example 4: equal elements in two arrays in c++
bool equalelementsintwoarrays(int A[], int B[], int N) {
sort(A, A+N);
sort(B, B+N);
int i = 0, j = 0;
while (i < N && j < N) {
if (A[i] == B[j]) return true;
else if (A[i] > B[j]) j++;
else i++;
}
return false;
}