Write a function `deepEqual(a, b)` that performs a deep comparison of two values (objects, arrays, primitives).
Problem Statement
Write a function `deepEqual(a, b)` that performs a deep comparison of two values (objects, arrays, primitives).
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Handle primitives first with strict equality.
š” Hint 2:
For objects/arrays, compare keys and recurse on values.
š” Hint 3:
Check same type, same number of keys, then recursively compare each key value pair.
ā
Solution:
```javascript
function deepEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (typeof a !== typeof b) return false;
if (typeof a !== "object") return false;
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
return keysA.every(key => deepEqual(a[key], b[key]));
}
```