Given two values o1 and o2, return a boolean value indicating whether two values, o1 and o2, are deeply equal.
For two values to be deeply equal, the following conditions must be met:
If both values are primitive types, they are deeply equal if they pass the === equality check.
If both values are arrays, they are deeply equal if they have the same elements in the same order, and each element is also deeply equal according to these conditions.
If both values are objects, they are deeply equal if they have the same keys, and the associated values for each key are also deeply equal according to these conditions.
You may assume both values are the output of JSON.parse. In other words, they are valid JSON.
Please solve it without using lodash's _.isEqual() function
Example 1:
Input: o1 = {"x":1,"y":2}, o2 = {"x":1,"y":2}
Output: true
Explanation: The keys and values match exactly.
Example 2:
Input: o1 = {"y":2,"x":1}, o2 = {"x":1,"y":2}
Output: true
Explanation: Although the keys are in a different order, they still match exactly.
Example 3:
Input: o1 = {"x":null,"L":[1,2,3]}, o2 = {"x":null,"L":["1","2","3"]}
Output: false
Explanation: The array of numbers is different from the array of strings.
Example 4:
Input: o1 = true, o2 = false Output: false Explanation: true !== false
Constraints:
1 <= JSON.stringify(o1).length <= 1051 <= JSON.stringify(o2).length <= 105maxNestingDepth <= 1000Problem Overview: You’re given two JSON values and must determine if they are deeply equal. Equality means primitives match exactly, arrays contain equal elements in the same order, and objects contain identical keys with recursively equal values.
Approach 1: JSON.stringify Comparison (O(n) time, O(n) space)
A quick idea is converting both JSON values into strings using JSON.stringify() and comparing the results. If the serialized strings match, the structures appear equal. This works for many simple cases because the entire structure is flattened into a string representation. However, object key order can differ while still representing the same JSON structure. Since JSON.stringify() preserves insertion order, two logically equal objects may serialize differently and produce a false negative.
This approach is occasionally useful for quick checks or controlled environments where object key order is guaranteed. It’s not reliable for a general deep equality check.
Approach 2: Recursive Deep Comparison (DFS) (O(n) time, O(n) space)
The correct solution walks both JSON structures simultaneously using recursion. Start by checking primitive values with strict equality. If both values are arrays, verify their lengths match and recursively compare each element at the same index. If both are objects, compare their key sets first; differing key counts immediately indicate inequality.
After confirming both objects contain the same keys, recursively compare the value for each key. This forms a depth‑first traversal across the entire JSON structure. Every nested value is checked exactly once, producing O(n) time complexity where n is the total number of elements and properties. Recursion depth contributes up to O(n) space in the worst case for deeply nested data.
This pattern is essentially a DFS over a tree-like structure and mirrors problems involving recursion and depth-first search. Objects behave similarly to key-value maps, conceptually related to hash map comparisons.
Approach 3: Iterative Stack Traversal (O(n) time, O(n) space)
The recursive approach can be rewritten using an explicit stack. Push pairs of values that must be compared. Pop a pair, check their types, and push nested elements or key-value pairs when necessary. Arrays push element pairs by index, while objects push pairs for each shared key.
This version avoids recursion depth limits and makes the traversal logic explicit. Complexity remains identical because every node in the JSON structure is processed once.
Recommended for interviews: Recursive deep comparison is the expected solution. It clearly demonstrates understanding of structural equality, recursion, and careful handling of arrays versus objects. Mentioning the JSON.stringify shortcut shows awareness of edge cases, but implementing the recursive comparison shows real problem-solving skill.
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| JSON.stringify Comparison | O(n) | O(n) | Quick checks where object key order is guaranteed and structures are simple |
| Recursive Deep Comparison (DFS) | O(n) | O(n) | General case; safest and most common interview solution |
| Iterative Stack Traversal | O(n) | O(n) | When avoiding recursion depth limits or implementing explicit traversal |
JSON Deep Equal - Leetcode 2628 - JavaScript 30-Day Challenge • NeetCodeIO • 5,797 views views
Watch 3 more video solutions →Practice JSON Deep Equal with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor