constarray1=[1,2,3,4,5];constarray2=[1,2,3,4,5];// This is the functionconstcompare=(a1,a2)=>a1.length==a2.length&&a1.every((element,index)=>element===a2[index]);// Exampleconsole.log(compare(array1,array2));// Sortedconsole.log(compare(array1.sort(),array2.sort()));
Whether that's a drawback or by design is questionable; whether two identical objects should be treated as actually the same completely depends on your problem domain.
Software Developer
2 years of teaching in Java, Spring Boot, Software Architectures
Working with Spring Boot, Node JS, Docker
Currently learning Tensorflow
Correct Algorithm
Here is the Correct Algorithm
You forgot to compare array sizes.
If a2 has more elements than a1, then this algo fails.
Thanks, Now it's upadated.
Yes but we are talking about the simple approach here
And your function has some drawbacks also
Whether that's a drawback or by design is questionable; whether two identical objects should be treated as actually the same completely depends on your problem domain.
agreed 100%
In js your Objects are not equal
If your arrays are
It works.
I think it would be greate if we sort the array inside compare function, so we can sure that it will compare with the right position
This was the solution for a subtask of a problem I recently solved in a project.
I compared two vectors element-wise and counted the amount of distinct elements