Differences Between Two Objects - Problem

Given two deeply nested objects or arrays obj1 and obj2, write a function that identifies and returns their differences. This is like creating a "diff" tool for JSON structures!

Your function should:

  • ๐Ÿ” Compare all properties recursively through nested structures
  • ๐Ÿ“Š Return only changed values in the format [obj1Value, obj2Value]
  • ๐Ÿšซ Ignore keys that exist in only one object
  • ๐ŸŒณ Preserve the nested structure in the result

Special note: When comparing arrays, treat indices as keys. Both inputs are guaranteed to be valid JSON structures.

Example: If obj1 = {"a": 1, "b": {"x": 2}} and obj2 = {"a": 3, "b": {"x": 2}}, return {"a": [1, 3]}

Input & Output

example_1.js โ€” Basic Object Comparison
$ Input: obj1 = {"a": 5, "b": {"c": "hello"}} obj2 = {"a": 5, "b": {"c": "world"}}
โ€บ Output: {"b": {"c": ["hello", "world"]}}
๐Ÿ’ก Note: The property 'a' has the same value (5) in both objects, so it's not included. The nested property 'b.c' differs ('hello' vs 'world'), so we return the nested structure with the difference array.
example_2.js โ€” Array Comparison
$ Input: obj1 = [1, 2, {"x": 3}] obj2 = [1, 4, {"x": 3}]
โ€บ Output: [null, [2, 4]]
๐Ÿ’ก Note: Index 0 has the same value (1), so it becomes null. Index 1 differs (2 vs 4), creating a difference array. Index 2 has identical objects, so it becomes null. Trailing nulls are cleaned up in the final result.
example_3.js โ€” No Differences
$ Input: obj1 = {"name": "John", "age": 30} obj2 = {"name": "John", "age": 30}
โ€บ Output: {}
๐Ÿ’ก Note: All properties have identical values, so no differences are found. The function returns an empty object indicating no changes between the two objects.

Visualization

Tap to expand
Object Difference Detection ProcessObject 1{"name": "Alice", "age": 25, "city": "NYC"}Object 2{"name": "Alice", "age": 30, "city": "LA"}Differences{"age": [25, 30], "city": ["NYC", "LA"]}โœ“name: "Alice" = "Alice"โ‰ age: 25 โ‰  30โ‰ city: "NYC" โ‰  "LA"
Understanding the Visualization
1
Initial Setup
Start with two objects/arrays and prepare to compare them recursively
2
Type & Reference Check
Quick validation: are they the same type? Same reference? If identical, skip processing
3
Key/Index Iteration
For objects: iterate through keys. For arrays: iterate through indices. Only process common keys/indices
4
Recursive Comparison
For each common key/index, recursively compare the values. If both are objects/arrays, dive deeper
5
Difference Recording
When primitive values differ, record them as [obj1Value, obj2Value] in the result structure
6
Result Optimization
Clean up the result: remove trailing nulls from arrays, return empty object if no differences found
Key Takeaway
๐ŸŽฏ Key Insight: The algorithm efficiently identifies differences by traversing both structures simultaneously, creating a result that mirrors the original structure but contains only the changed values as difference arrays.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Linear time complexity where n is the number of differing properties. Early termination reduces constant factors significantly.

n
2n
โœ“ Linear Growth
Space Complexity
O(d)

Space complexity depends on recursion depth d and number of actual differences, not total properties.

n
2n
โœ“ Linear Space

Constraints

  • Both objects are valid JSON structures (output of JSON.parse)
  • Maximum nesting depth: 100 levels
  • Maximum number of properties per object: 104
  • Property keys are strings with maximum length 100
  • Arrays are compared by index position
  • Only common keys/indices are considered for comparison
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28
42.3K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen