LeetCode 2755: Deep Merge of Two Objects Solution

Master LeetCode problem 2755 (Deep Merge of Two Objects), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2755. Deep Merge of Two Objects

Medium

Problem Explanation

Explanation:

To perform a deep merge of two objects, we need to combine the key-value pairs of the two objects in a way that handles nested objects recursively. If both objects have a key with the same name, we need to merge their values based on the following rules:

  • If both values are primitive types (int, string, etc.), the value from the second object should overwrite the value from the first object.
  • If both values are objects, we should recursively merge them.

The algorithm involves iterating through the keys of both objects and recursively merging their values if they are objects. We also need to handle the case where one key is present in one object but not in the other.

Time Complexity: O(n), where n is the total number of keys in both objects. Space Complexity: O(n), for the space used by the recursive calls.

: :

Solution Code

import java.util.HashMap;
import java.util.Map;

public class DeepMerge {

    public static Map<String, Object> deepMerge(Map<String, Object> obj1, Map<String, Object> obj2) {
        Map<String, Object> merged = new HashMap<>(obj1);

        for (String key : obj2.keySet()) {
            if (obj1.containsKey(key) && obj1.get(key) instanceof Map && obj2.get(key) instanceof Map) {
                merged.put(key, deepMerge((Map<String, Object>) obj1.get(key), (Map<String, Object>) obj2.get(key)));
            } else {
                merged.put(key, obj2.get(key));
            }
        }

        return merged;
    }

    public static void main(String[] args) {
        Map<String, Object> obj1 = new HashMap<>();
        obj1.put("a", 1);
        obj1.put("b", "hello");
        obj1.put("c", Map.of("x", 5));

        Map<String, Object> obj2 = new HashMap<>();
        obj2.put("a", 2);
        obj2.put("b", "world");
        obj2.put("c", Map.of("y", 10));

        Map<String, Object> merged = deepMerge(obj1, obj2);
        System.out.println(merged);
    }
}

Try It Yourself

Loading code editor...

Frequently Asked Questions

How to solve LeetCode 2755 (Deep Merge of Two Objects)?

This page provides optimized solutions for LeetCode problem 2755 (Deep Merge of Two Objects) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2755 (Deep Merge of Two Objects)?

The time complexity for LeetCode 2755 (Deep Merge of Two Objects) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2755 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2755 in Java, C++, or Python.

Back to LeetCode Solutions