Is There A Way To Check If Two Arrays Have The Same Elements?
Solution 1:
Using jQuery
You can compare the two arrays using jQuery
:
// example arrays:var firstArray = [ 1, 2, 3, 4, 5 ];
var secondArray = [ 5, 4, 3, 2, 1 ];
// compare arrays:var isSameSet = function( arr1, arr2 ) {
return $( arr1 ).not( arr2 ).length === 0 && $( arr2 ).not( arr1 ).length === 0;
}
// get comparison result as boolean:var result = isSameSet( firstArray, secondArray );
Here is a JsFiddle Demo
See this question helpful answer
Solution 2:
Well there is an Array.sort()
method in JavaScript, and for comparing the (sorted) arrays, I think it's best to check out this question, as it is has a really good answer.
Especially note that comparing arrays as strings (e.g. by JSON.stringify
) is a very bad idea, as values like "2,3"
might break such a check.
Solution 3:
Suppose you have:
const xs = [1,2,3];
const ys = [3,2,1];
This seems to work:
xs.every(x => ys.includes(x));
//=> true
But it gives you false positives:
const xs = [2,2,2];
const ys = [1,2,3];
xs.every(x => ys.includes(x));
//=> true// But…
ys.every(y => xs.includes(y));
//=> false
Another example:
const xs = [2];
const ys = [1,2,3];
xs.every(x => ys.includes(x));
//=> true// But…
ys.every(y => xs.includes(y));
//=> false
We could compare the size of both arrays and bail out quickly but technically these two arrays do contain the same elements:
const xs = [2];
const ys = [2,2,2];
ys.every(y => xs.includes(y));
//=> true
xs.every(x => ys.includes(x));
//=> true
The way I would answer this question is by computing the set of unique values for both arrays.
const similar = (xs, ys) => {
const xsu = [...newSet(xs).values()]; // unique values of xsconst ysu = [...newSet(ys).values()]; // unique values of ysreturn xsu.length != ysu.length ? false : xsu.every(x => ysu.includes(x));
}
similar([1,2,3],[3,2,1]);
//=> true
similar([2,2,2],[3,2,1]);
//=> false
similar([2],[3,2,1]);
//=> false
similar([2],[2,2,2]);
//=> true
similar([1,2,3],[4,5,6]);
//=> false
Solution 4:
Here's a working implementation using Vanilla JS:
functionhaveMatchingElements(firstArray, secondArray) {
var stringsInFirstArray = parse(firstArray, 'string'),
stringsInSecondArray = parse(secondArray, 'string'),
numbersInFirstArray = parse(firstArray, 'number'),
numbersInSecondArray = parse(secondArray, 'number'),
stringResults = compare(stringsInFirstArray, stringsInSecondArray),
numberResults = compare(numbersInFirstArray, numbersInSecondArray);
if (stringResults && numberResults) {
returntrue;
} returnfalse;
functionparse(array, type) {
var arr = [];
arr = array.sort().filter(function(index) {
if (typeof index == type)
return index;
});
return arr;
}
functioncompare(firstArray, secondArray) {
if (firstArray.length !== secondArray.length)
returnfalse;
for (var i = firstArray.length; i--;) {
if (firstArray[i] !== secondArray[i])
returnfalse;
}
returntrue;
}
}
This parses strings an numbers into different arrays and checks them separately. That will correct the issue of 1
and "1"
matching as true
due to the implicit type conversion caused by the sort
function.
The implementation is simple:
var arr1 = ['1', 1];
var arr2 = [1, '1'];
var results = haveMatchingElements(arr1, arr2);
console.log(results); // true
Solution 5:
Using Vanilla JavaScript
Supported in all modern browsers you can use Array.prototype.every()
ECMAScript 2016
let firstArray = [1, 2, 3, 4, 5];
let secondArray = [5, 4, 3, 2, 1];
letEquals = firstArray.every((item)=>secondArray.includes(item))
alert("Equals? " + Equals)
Internet Explorer Support
Internet Explorer does not have access to Array.prototype.includes(). If you need it to run in Internet Explorer you can use indexOf
let firstArray = [1, 2, 3, 4, 5];
let secondArray = [5, 4, 3, 2, 1];
letEquals = firstArray.every(function (item){return secondArray.indexOf(item) > -1})
alert("Equals? " + Equals)
This will iterate through every item in firstArray and check if the value is contained within secondArray, and return true only if the function returns true for Every item
Do note that the given function will work for this question, But is Non Recursive and only works with primitive types on two Flat arrays, If you want to compare non-primitives you will need to modify the compare function to compare your Object structure
Post a Comment for "Is There A Way To Check If Two Arrays Have The Same Elements?"