Join Two Arrays by ID Problem
Description
LeetCode Problem 2722.
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
arr2should override the value fromarr1.
Example 1:
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:
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:
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:
arr1andarr2are valid JSON arrays- Each object in
arr1andarr2has a unique integeridkey 2 <= JSON.stringify(arr1).length <= 10^62 <= JSON.stringify(arr2).length <= 10^6
Solution
Approach: Map keyed by id
Put the objects of arr1 into a Map keyed by id. Then go through arr2: if its id is already in the map, merge the two objects with the spread operator so that arr2's fields overwrite arr1's (and fields only in arr1 survive); otherwise just insert it.
Finally take the map's values and sort them by id ascending. The sort is needed because a Map keeps insertion order, and the final order must not depend on which array an id came from.
Complexity: Time O(n log n) for the final sort (n = total number of objects), space O(n).
/**
* @param {Array} arr1
* @param {Array} arr2
* @return {Array}
*/
var join = function (arr1, arr2) {
const byId = new Map();
for (const item of arr1) {
byId.set(item.id, item);
}
for (const item of arr2) {
byId.set(item.id, { ...byId.get(item.id), ...item });
}
return [...byId.values()].sort((a, b) => a.id - b.id);
};