2722. Join Two Arrays by ID LeetCode Solution
In this guide, you will get 2722. Join Two Arrays by ID LeetCode Solution with the best time and space complexity. The solution to Join Two Arrays by ID problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Join Two Arrays by ID solution in C++
- Join Two Arrays by ID solution in Java
- Join Two Arrays by ID solution in Python
- Additional Resources

Problem Statement of Join Two Arrays by ID
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.
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:
arr1 and arr2 are valid JSON arrays
Each object in arr1 and arr2 has a unique integer id key
2 <= JSON.stringify(arr1).length <= 106
2 <= JSON.stringify(arr2).length <= 106
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
2722. Join Two Arrays by ID LeetCode Solution in C++
type JSONValue =
| {}
| null
| boolean
| number
| string
| JSONValue[]
| { [key: string]: JSONValue };
type Item = { [key: string]: JSONValue } & {
id: number;
};
function join(arr1: Item[], arr2: Item[]): Item[] {
const idToObj: { [key: number]: Item } = {};
for (const item of arr1) {
idToObj[item.id] = item;
}
for (const item of arr2) {
if (idToObj[item.id] === undefined) {
idToObj[item.id] = item;
} else {
for (const key of Object.keys(item)) {
idToObj[item.id][key] = item[key];
}
}
}
const ans = Object.values(idToObj);
ans.sort((a, b) => a.id - b.id);
return ans;
}
/* code provided by PROGIEZ */
2722. Join Two Arrays by ID LeetCode Solution in Java
N/A
// code provided by PROGIEZ
2722. Join Two Arrays by ID LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.