I have the following arrays,
var a = [1,2,3,4,5];
var b = [2,3,4,5,6];
var c = [4,5,6,7,8];
var d = [1,2,3,4,5];
What is the most efficient way to find out the arrays that are distinct? I.e array a, b and c are distinct, where the order matters.
I have the following arrays,
var a = [1,2,3,4,5];
var b = [2,3,4,5,6];
var c = [4,5,6,7,8];
var d = [1,2,3,4,5];
What is the most efficient way to find out the arrays that are distinct? I.e array a, b and c are distinct, where the order matters.
You can use Array.prototype.every() to compare arrays with Javascript
var a = [1,2,3,4,5];
var b = [2,3,4,5,6];
var is_same = (a.length == b.length) && a.every(function(element, index) {
return element === b[index];
});
One interesting way would be to convert them to String and Compare them. You could JSON stringify
them or just join
them like this
a.join('') === b.join('')
This works just because you say the order matters. I don't know the benchmarks between using JSON's stringify over join primitive. Maybe you could try that.
This is also can be done like this
a.toString() === b.toString()