Deep Equal
MediumP1Coding
Write deepEqual(a, b), which returns true if two JSON-style values are structurally equal.
The values are made of numbers, strings, booleans, null, arrays and plain objects, nested to any depth.
- Primitives are equal when they have the same type and value:
1and"1"differ,0andfalsediffer,nulland0differ. - Arrays are equal when they have the same length and equal elements at each index. Order matters.
- Objects are equal when they have the same set of keys and equal values for each key. Key order does not matter.
- An array never equals an object, even when both are empty.
Example 1
- Input
- a = {"a":1,"b":[1,2]}, b = {"b":[1,2],"a":1}
- Output
- true
Example 2
- Input
- a = [1,2], b = [2,1]
- Output
- false
Example 3
- Input
- a = [], b = {}
- Output
- false
Constraints
- The total size of each value is at most 10^4 elements.
- Nesting depth is at most 1000.
- No cycles.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 17. ⌘Enter runs, ⌘⇧Enter submits.