Write deepEqual(a, b) for JSON values and print whether the two inputs are deeply equal.
Two values are deeply equal when:
- both are the same primitive (number, string, boolean or
null) with the same value and type — no coercion, so1and"1"differ, andtrueand1differ; - both are arrays of the same length whose elements are deeply equal position by position;
- both are objects (not arrays) with exactly the same set of keys, and deeply equal values for each key. Key order does not matter.
Constraints
- Each line is valid JSON of at most 5000 characters.
- Numbers are integers. Objects have no duplicate keys.
Input and output
- Input
- Line 1: the first JSON value Line 2: the second JSON value
- Output
- `true` or `false`
Examples
Input 1
{"a":1,"b":[1,2,{"c":true}]}
{"b":[1,2,{"c":true}],"a":1}Output
trueSame keys and values; object key order does not matter.
Input 2
[1,2,3]
[1,3,2]Output
falseArray order matters.
Input 3
{"n":1}
{"n":"1"}Output
falseThe number 1 and the string "1" are different (no coercion).
Show hints
- Handle null before checking typeof "object".
- An array and an object can have the same keys (0, 1, …), so test Array.isArray on both sides.
Show the explanation
Recursive comparison:
- If
a === b, they are equal (covers equal primitives). - If either is not an object, or either is
null, they are not equal (different primitives, or primitive vs object). - If one is an array and the other is not, not equal.
- Arrays: same length and every element pair deeply equal.
- Objects: the same number of keys, and every key of
aexists as an own key inbwith a deeply equal value.
Checking own keys matters: { a: undefined } is not the same as {} in JavaScript, and {"a": null} is not the same as {} in JSON. Libraries such as Lodash's isEqual also handle dates, maps, sets and cycles, which plain JSON does not have.
Original UniCircle question. Last reviewed 22 Oct 2026.
