JavaScript Program to Check if Two Arrays are Equal or Not
Last Updated :
05 Jun, 2024
Given two arrays, arr1 and arr2 of equal length N, the task is to find if the given arrays are equal or not.
Two arrays are said to be equal if:
- Both of them contain the same set of elements,
- Arrangements (or permutations) of elements might/might not be the same.
- If there are repetitions, then counts of repeated elements must also be the same for two arrays to be equal.
Examples:
Input: arr1[] = {1, 2, 5, 4, 0}, arr2[] = {2, 4, 5, 0, 1}
Output: Yes
Input: arr1[] = {1, 2, 5, 4, 0, 2, 1}, arr2[] = {2, 4, 5, 0, 1, 1, 2}
Output: Yes
Input: arr1[] = {1, 7, 1}, arr2[] = {7, 7, 1}
Output: No
Sort two arrays and then compare their elements sequentially. If all elements in both arrays match, return true; otherwise, return false. This process ensures that both arrays have the same elements in the same order, confirming their equality.
Example: Below is the implementation:
JavaScript
function equalArr(arr1, arr2) {
const N = arr1.length;
const M = arr2.length;
if (N !== M) return false;
arr1.sort();
arr2.sort();
for (const [i, ele] of arr1.entries()) {
if (ele !== arr2[i]) return false;
}
return true;
}
let arr1 = [3, 5, 2, 5, 2];
let arr2 = [2, 3, 5, 5, 2];
if (equalArr(arr1, arr2)) {
console.log("Yes");
} else {
console.log("No");
}
First check if length of arr1 is not equal to the length of arr2 then return false. Then traverse over first array and store the count of every element in the hash map. Then traverse over second array and decrease the count of its elements in the hash map. Hash map, then return false, else decrease the count of that element. Return true at the end, as both the arrays are equal by now.
Example: Below is the implementation:
JavaScript
// Javascript program to find given two array
// are equal or not using hashing technique
// Returns true if arr1[0..N-1] and arr2[0..M-1]
// contain same elements.
function areEqual(arr1, arr2) {
let N = arr1.length;
let M = arr2.length;
// If lengths of arrays are not equal
if (N != M)
return false;
// Store arr1[] elements and their counts in
// hash map
let map
= new Map();
let count = 0;
for (let i = 0; i < N; i++) {
if (map.get(arr1[i]) == null)
map.set(arr1[i], 1);
else {
count = map.get(arr1[i]);
count++;
map.set(arr1[i], count);
}
}
// Traverse arr2[] elements and check if all
// elements of arr2[] are present same number
// of times or not.
for (let i = 0; i < N; i++) {
// If there is an element in arr2[], but
// not in arr1[]
if (!map.has(arr2[i]))
return false;
// If an element of arr2[] appears more
// times than it appears in arr1[]
if (map.get(arr2[i]) == 0)
return false;
count = map.get(arr2[i]);
--count;
map.set(arr2[i], count);
}
return true;
}
// Driver code
let arr1 = [3, 1, 2, 5, 2];
let arr2 = [2, 3, 5, 5, 2];
// Function call
if (areEqual(arr1, arr2))
console.log("Yes");
else
console.log("No");
Approach 3: Using Frequency Map with Reduce
This approach also involves creating frequency maps for both arrays and then comparing these maps. Instead of iterating manually to build the frequency maps, we can use the reduce method to streamline the process.
Example:
JavaScript
function areArraysEqual(arr1, arr2) {
if (arr1.length !== arr2.length) return false;
const frequencyMap1 = arr1.reduce((acc, val) => {
acc[val] = (acc[val] || 0) + 1;
return acc;
}, {});
const frequencyMap2 = arr2.reduce((acc, val) => {
acc[val] = (acc[val] || 0) + 1;
return acc;
}, {});
for (let key in frequencyMap1) {
if (frequencyMap1[key] !== frequencyMap2[key]) {
return false;
}
}
return true;
}
// Driver code
let arr1 = [3, 1, 2, 5, 2];
let arr2 = [2, 3, 5, 1, 2];
if (areArraysEqual(arr1, arr2)) {
console.log("Yes");
} else {
console.log("No");
}
Similar Reads
JavaScript Program to Find the Distance Value between Two Arrays We will be given two integer arrays and an integer d. The task is to calculate the distance between the two given arrays based on the given integer value. The distance value will be calculated as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <=
3 min read
JavaScript- Arrays are Equal or Not These are the following approaches to compare two arrays in JavaScript:1. Using the JSON.stringify() MethodJavaScript provides a function JSON.stringify() method in order to convert an object whether or array into a JSON string. By converting it into JSON strings, we can directly check if the string
4 min read
How to Compare Two Arrays in PHP? Given two arrays i.e. Array1, and Array2, the task is to compare both arrays in PHP. The comparison checks for equality finds differences, or determines common elements. PHP offers several functions and methods to compare arrays effectively. Below are the approaches to compare two arrays in PHP: Tab
2 min read
Comparing two ArrayList In Java Java provides a method for comparing two Array List. The ArrayList.equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal. Example: Input : ArrayLis
2 min read
Java Program to Compare two Boolean Arrays Two arrays are equal if they contain the same elements in the same order. In java, we can compare two Boolean Arrays in 2 ways: By using Java built-in method that is .equals() method.By using the Naive approach. Examples: Input : A = [true , true , false] A1 = [true, true, false] Output: Both the ar
3 min read