Created
April 8, 2019 19:01
-
-
Save YannickLeRoux/0aa9a5374a48d50f18a75bb13c9232ae to your computer and use it in GitHub Desktop.
Check the equality of 2 arrays or that both contain same elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compares 2 arrays | |
const arraysEqual = (a, b) => { | |
if (a === b) return true; | |
if (a == null || b == null) return false; | |
if (a.length !== b.length) return false; | |
// compare that both contains same elements no matter the order (optional) | |
const aSort = a.sort(); | |
const bSort = b.sort(); | |
for (let i = 0; i < a.length; ++i) { | |
if (aSort[i] !== bSort[i]) return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment