Deep Merge

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Deep Merge

Approach

Iterate over each source object's keys. When both target and source values are plain objects, recursively merge them. When both are arrays, concatenate them. Otherwise, the source value overwrites the target. Process multiple sources left to right.

Complexity Analysis

Time
O(n) where n is the total number of properties
Space
O(n)

Pattern

Deep Merge

Why It Works

Recursing into nested plain objects preserves deeply nested values from both sides, while array concatenation and scalar overwriting handle the other cases predictably.

Updated Mar 2026