JSON Deep Equal Problem
#2628
Description
LeetCode Problem 2628.
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 <= 10^51 <= JSON.stringify(o2).length <= 10^5maxNestingDepth <= 1000
Solution
Approach: Recursive comparison
Compare in this order:
- If
o1 === o2, they are equal. This covers all equal primitives,nulland the same reference. - If either value is
nullor not an object, they are different (a primitive that failed===, or a primitive against an object). - If one is an array and the other is a plain object, they are different.
Object.keyswould otherwise treat[1]and{"0": 1}as the same. - Both are arrays or both are objects: the key counts must match, and every key of
o1must exist ino2with a deeply equal value. Arrays are handled by the same code, since their keys are the indices0..length-1, which also checks order and length.
Key order does not matter for objects, because we look keys up rather than walking both key lists in parallel. Checking the key count first means o2 cannot have extra keys.
Complexity: O(m) time over the total size m of the two values; O(d) recursion depth for nesting depth d.
/**
* @param {null|boolean|number|string|Array|Object} o1
* @param {null|boolean|number|string|Array|Object} o2
* @return {boolean}
*/
var areDeeplyEqual = function (o1, o2) {
if (o1 === o2) return true;
if (o1 === null || o2 === null) return false;
if (typeof o1 !== 'object' || typeof o2 !== 'object') return false;
if (Array.isArray(o1) !== Array.isArray(o2)) return false;
const keys1 = Object.keys(o1);
if (keys1.length !== Object.keys(o2).length) return false;
for (const key of keys1) {
if (!Object.prototype.hasOwnProperty.call(o2, key)) return false;
if (!areDeeplyEqual(o1[key], o2[key])) return false;
}
return true;
};