Check if Two Arrays Are Equal
Arguments
a: any[] —The first array to compare. b: any[] —The second array to compare.
Returns
boolean — true if both arrays are identical in length and elements (in the same order), otherwise false.Solution idea
Compare arrays by first checking length and then scanning index-by-index for strict equality. This is the standard shallow equality test when arrays contain primitives or stable references, and it avoids expensive conversions or nested loops.
// Basic examples
arraysEqual([1, 2, 3], [1, 2, 3]) // => true
arraysEqual([1, 2, 3], [3, 2, 1]) // => false
// Different lengths
arraysEqual([1, 2], [1, 2, 3]) // => false
// Works with strings
arraysEqual(['a', 'b'], ['a', 'b']) // => true
// Works with mixed types
arraysEqual([1, '2', 3], [1, 2, 3]) // => falseSolution
Overview
Compare arrays by first checking length and then scanning index-by-index for strict equality. This is the standard shallow equality test when arrays contain primitives or stable references, and it avoids expensive conversions or nested loops.
Approach 1: Loop-based comparison (explicit and efficient)
Algorithm:
1) If a === b, return true (same reference).
2) If either is not an array or lengths differ, return false.
3) Loop through indices; if a[i] !== b[i], return false.
4) If the loop completes, return true.
Complexity: O(n) time, O(1) space. This intentionally does not deep-compare nested objects.
export default function arraysEqual(a, b) {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
export default function arraysEqual(a: any[], b: any[]): boolean {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
Approach 2: Using every() (declarative style)
Idea: use Array.prototype.every() to ensure that every element in a matches the element at the same index in b.
How it works:
- If
a.length !== b.length, returnfalse. - Otherwise, return
a.every((el, i) => el === b[i]).
Why use it: more declarative and concise than a manual loop, while still running in O(n) time and short-circuiting on the first mismatch.
export default function arraysEqual(a, b) {
return a.length === b.length && a.every((el, i) => el === b[i]);
}
export default function arraysEqual(a: any[], b: any[]): boolean {
return a.length === b.length && a.every((el, i) => el === b[i]);
}
Approach 3: JSON.stringify shortcut (not recommended for complex data)
Idea: serialize both arrays to JSON strings and compare the resulting strings.
How it works:
- Compute
JSON.stringify(a)andJSON.stringify(b). - Compare the two strings with
===and return the result.
Why use it: very compact and can work for simple arrays of primitives, but it has important caveats — it does not handle undefined, functions, or differences in object key order reliably, so it is not suitable for robust deep equality checks.
export default function arraysEqual(a, b) {
return JSON.stringify(a) === JSON.stringify(b);
}
export default function arraysEqual(a: any[], b: any[]): boolean {
return JSON.stringify(a) === JSON.stringify(b);
}
Notes & Pitfalls
Strict equality ( ===) does not perform deep comparison for objects or nested arrays; it compares references.Avoid relying on JSON.stringifyfor nested structures, objects with varying key order, or values likeundefined, functions, or symbols.Be aware that ['1']and[1]are not considered equal due to type differences.
Empty arrays → true.Different lengths → false.Different order → false.Mixed types → falsewhen any pair differs by value or type.Nested arrays or objects → compared by reference in Approaches 1 and 2; not reliably by structure in Approach 3.
Using index-based loops for element-wise comparison. Using every()for a declarative comparison with early exit.Understanding reference vs. value equality in JavaScript.
Common mistakes on this challenge
Array equality contract should be strict and index-based
Equality here means same length and same value at each index with strict comparison. Return false early when lengths differ. Compare each index with strict equality ( !==).Do not sort arrays before comparison; order matters.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.