Compare two arrays for equality, ignoring order, in JavaScript
This post will discuss how to compare two arrays for equality, ignoring order, in JavaScript. In other words, check if two arrays have the same elements in any order in JavaScript.
To compare two arrays for equality, ignoring order, we need to check if they have the same length and the same elements, regardless of their positions. There are several methods to do this:
1. Using a Map
This function creates a Map object that stores the frequency of each element in the first array, and then iterates over the second array and decrements the frequency of each element. If the Map object has any non-zero values at the end, it means that the arrays are not equal. This method works well for arrays of any type, but it may not handle NaN values correctly. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
function isEqual(a, b) { // check if the lengths are equal if (a.length !== b.length) { return false; } let map = new Map(); for (let elem of a) { // increment the frequency of each element map.set(elem, (map.get(elem) || 0) + 1); } for (let elem of b) { // if the element is not in the map, the arrays are not equal if (!map.has(elem)) { return false; } // decrement the frequency of each element map.set(elem, map.get(elem) - 1); // if the frequency becomes negative, the arrays are not equal if (map.get(elem) < 0) { return false; } } return true; } const arr1 = [1, 2, 3]; const arr2 = [3, 2, 1]; const arr3 = [1, 2, 4]; console.log(isEqual(arr1, arr2)); // true (same elements in any order) console.log(isEqual(arr1, arr3)); // false (different elements) |
2. Using a custom function
This function defines a custom function that can compare two values of any type, including nested arrays or objects. The function uses recursion to check if each element in the first array is present in the second array, and vice versa. This function works well for arrays of any type and structure, but it may be slower than other functions. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
// A custom function that can compare two values of any type function isEqual(a, b) { // if the values are strictly equal, return true if (a === b) { return true; } // if the values have different types, return false if (typeof a !== typeof b) { return false; } // if both values are arrays if (Array.isArray(a) && Array.isArray(b)) { // if they have different lengths, return false if (a.length !== b.length) { return false; } // for each element in a for (let elem of a) { // a flag to indicate if the element is found in b let found = false; // for each element in b for (let other of b) { // recursively compare the elements if (isEqual(elem, other)) { // if they are equal, set the flag to true and break the loop found = true; break; } } // if the element is not found in b, return false if (!found) return false; } // otherwise return true return true; } // if both values are objects if (typeof a === "object" && typeof b === "object") { // get the keys of a let keys1 = Object.keys(a); // get the keys of b let keys2 = Object.keys(b); // compare the keys as sorted arrays if (!isEqual(keys1.sort(), keys2.sort())) { return false; } // for each key in a for (let key of keys1) { // recursively compare the values at that key if (!isEqual(a[key], b[key])) { return false; } } // otherwise return true return true; } // if none of the above cases match, return false return false; } const arr1 = [1, 2, 3]; const arr2 = [3, 2, 1]; const arr3 = [1, 2, 4]; console.log(isEqual(arr1, arr2)); // true (same elements in any order) console.log(isEqual(arr1, arr3)); // false (different elements) |
3. Using a Set
To check if two arrays have the same elements in any order in JavaScript, we can use the Set object, which is a collection of unique values. A Set can be created from an array, and it will automatically remove any duplicate elements. We can then compare the size and the elements of two sets to determine if they are equal. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
function isEqual(a, b) { // If the other array is a falsy value, return false if (!a || !b) { return false; } // Compare lengths - can save a lot of time if (a.length !== b.length) { return false; } // Convert both arrays to sets let set1 = new Set(a); let set2 = new Set(b); // Compare sizes - can save a lot of time if (set1.size != set2.size) { return false; } // Convert one set to an array and check if every element is in the other set return Array.from(set1).every(function (element) { return set2.has(element); }); } const arr1 = [1, 2, 3]; const arr2 = [3, 2, 1]; const arr3 = [1, 2, 4]; console.log(isEqual(arr1, arr2)); // true (same elements in any order) console.log(isEqual(arr1, arr3)); // false (different elements) |
This function works for arrays of primitive values or objects that can be compared by value, but not for nested arrays or objects that need to be compared by reference.
4. Using Array.every() and Array.includes() functions
These are built-in functions of the Array prototype that can help we check if two arrays have the same elements in any order. The Array.every() function takes a callback function that tests every element in the array and returns true if all elements pass the test. The Array.includes() function takes a value as an argument and returns true if the array contains that value. To check if two arrays have the same elements in any order, we can use these functions with a callback function that checks if every element in one array is included in the other array, and vice versa. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
function isEqual(a, b) { return a.length === b.length && a.every(element => b.includes(element)) && b.every(element => a.includes(element)); } const arr1 = [1, 2, 3]; const arr2 = [3, 2, 1]; const arr3 = [1, 2, 4]; console.log(isEqual(arr1, arr2)); // true (same elements in any order) console.log(isEqual(arr1, arr3)); // false (different elements) |
This function works for arrays of primitive values or objects that can be compared by value, but not for nested arrays or objects that need to be compared by reference.
5. Using Array.indexOf() function
This is another way to check if two arrays have the same elements in any order. The Array.indexOf() function takes a value as an argument and returns the first index at which that value can be found in the array, or -1 if it is not present. To check if two arrays have the same elements in any order, we can use a for loop to iterate over one array and use the indexOf() function to check if each element is present in the other array. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function isEqual(a, b) { // check if the lengths are equal if (a.length !== b.length) { return false; } // iterate over first array for (let i of a) { // if current element in the first array is not present in the second array if (b.indexOf(i) === -1) { return false; } } return true; } const arr1 = [1, 2, 3]; const arr2 = [3, 2, 1]; const arr3 = [1, 2, 4]; console.log(isEqual(arr1, arr2)); // true (same elements in any order) console.log(isEqual(arr1, arr3)); // false (different elements) |
This function is similar to the previous one, but it may be faster for some cases. However, it also does not check for deep equality of nested objects and arrays.
6. Using Array.sort() function
This function sorts both arrays in ascending order and then compares each element at the same index using the every() function. This function works well for arrays of numbers or strings, but it may not work for arrays of objects or other types. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function isEqual(a, b) { // If the other array is a falsy value, return false if (!a || !b) { return false; } // Compare lengths - can save a lot of time if (a.length !== b.length) { return false; } // Sort both arrays and compare them a.sort(), b.sort(); return a.every((val, index) => val === b[index]); } const arr1 = [1, 2, 3]; const arr2 = [3, 2, 1]; const arr3 = [1, 2, 4]; console.log(isEqual(arr1, arr2)); // true (same elements in any order) console.log(isEqual(arr1, arr3)); // false (different elements) |
This function is may not be very efficient for large arrays or complex values. It also does not check for deep equality of nested objects and arrays. It also mutate the original arrays.
That’s all about comparing arrays for equality, ignoring order, in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)