Problem

Given two arrays arr1 and arr2, return a new array joinedArray. All the objects in each of the two inputs arrays will contain an id field that has an integer value.

joinedArray is an array formed by merging arr1 and arr2 based on their id key. The length of joinedArray should be the length of unique values of id. The returned array should be sorted in ascending order based on the id key.

If a given id exists in one array but not the other, the single object with that id should be included in the result array without modification.

If two objects share an id, their properties should be merged into a single object:

  • If a key only exists in one object, that single key-value pair should be included in the object.
  • If a key is included in both objects, the value in the object from arr2 should override the value from arr1.

Examples

Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19

    
    
    Input: 
    arr1 = [
        {"id": 1, "x": 1},
        {"id": 2, "x": 9}
    ], 
    arr2 = [
        {"id": 3, "x": 5}
    ]
    Output: 
    [
        {"id": 1, "x": 1},
        {"id": 2, "x": 9},
        {"id": 3, "x": 5}
    ]
    Explanation: There are no duplicate ids so arr1 is simply concatenated with arr2.
    

Example 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20

    
    
    Input: 
    arr1 = [
        {"id": 1, "x": 2, "y": 3},
        {"id": 2, "x": 3, "y": 6}
    ], 
    arr2 = [
        {"id": 2, "x": 10, "y": 20},
        {"id": 3, "x": 0, "y": 0}
    ]
    Output: 
    [
        {"id": 1, "x": 2, "y": 3},
        {"id": 2, "x": 10, "y": 20},
        {"id": 3, "x": 0, "y": 0}
    ]
    Explanation: The two objects with id=1 and id=3 are included in the result array without modifiction. The two objects with id=2 are merged together. The keys from arr2 override the values in arr1.
    

Example 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14

    
    
    Input: 
    arr1 = [
        {"id": 1, "b": {"b": 94},"v": [4, 3], "y": 48}
    ]
    arr2 = [
        {"id": 1, "b": {"c": 84}, "v": [1, 3]}
    ]
    Output: [
        {"id": 1, "b": {"c": 84}, "v": [1, 3], "y": 48}
    ]
    Explanation: The two objects with id=1 are merged together. For the keys "b" and "v" the values from arr2 are used. Since the key "y" only exists in arr1, that value is taken form arr1.

Constraints

  • arr1 and arr2 are valid JSON arrays
  • Each object in arr1 and arr2 has a unique integer id key
  • 2 <= JSON.stringify(arr1).length <= 10^6
  • 2 <= JSON.stringify(arr2).length <= 10^6

Solution

Method 1 – Hash Map Merge and Sort

Intuition

We want to merge two arrays of objects by their id field, combining properties and letting arr2 override arr1 for duplicate keys. Using a hash map allows efficient merging and lookup.

Approach

  1. Create a hash map to store objects by their id.
  2. Insert all objects from arr1 into the map, using id as the key.
  3. For each object in arr2, merge it with the existing object in the map (if any), with arr2’s values taking precedence.
  4. Collect all merged objects, sort them by id in ascending order, and return as an array.

Code

1
2
3
4
5
6
function joinArraysById(arr1, arr2) {
  const mp = {};
  for (const obj of arr1) mp[obj.id] = { ...obj };
  for (const obj of arr2) mp[obj.id] = { ...(mp[obj.id] || {}), ...obj };
  return Object.values(mp).sort((a, b) => a.id - b.id);
}
1
2
3
4
5
6
function joinArraysById(arr1: Record<string, any>[], arr2: Record<string, any>[]): Record<string, any>[] {
  const mp: Record<string, Record<string, any>> = {};
  for (const obj of arr1) mp[obj.id] = { ...obj };
  for (const obj of arr2) mp[obj.id] = { ...(mp[obj.id] || {}), ...obj };
  return Object.values(mp).sort((a, b) => a.id - b.id);
}

Complexity

  • ⏰ Time complexity: O(n + m + k log k) — n and m are the lengths of arr1 and arr2, k is the number of unique ids (for sorting).
  • 🧺 Space complexity: O(n + m) — For the hash map and result array.