Join Two Arrays by ID - Problem

Given two arrays arr1 and arr2, return a new array joinedArray. All the objects in each of the two input 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.

Input & Output

Example 1 — Basic Merge
$ Input: arr1 = [{"id":1,"x":1},{"id":2,"x":2}], arr2 = [{"id":3,"x":3}]
Output: [{"id":1,"x":1},{"id":2,"x":2},{"id":3,"x":3}]
💡 Note: No overlapping IDs, so objects are combined and sorted by ID: 1, 2, 3
Example 2 — Property Override
$ Input: arr1 = [{"id":1,"x":1},{"id":2,"x":2}], arr2 = [{"id":1,"x":10,"y":20}]
Output: [{"id":1,"x":10,"y":20},{"id":2,"x":2}]
💡 Note: ID 1 exists in both arrays. arr2 overrides x:1 with x:10 and adds y:20
Example 3 — Complex Merge
$ Input: arr1 = [{"id":2,"a":1,"b":2},{"id":3,"a":3}], arr2 = [{"id":1,"c":4},{"id":2,"a":5}]
Output: [{"id":1,"c":4},{"id":2,"a":5,"b":2},{"id":3,"a":3}]
💡 Note: ID 2 merges: arr2's a:5 overrides arr1's a:1, but b:2 remains. Final order: 1,2,3

Constraints

  • 1 ≤ arr1.length, arr2.length ≤ 1000
  • Each object contains an id field with unique integer value within its array
  • 0 ≤ number of additional properties ≤ 10
  • Property values can be any valid JSON type

Visualization

Tap to expand
Join Two Arrays by ID INPUT arr1: {"id":1,"x":1} id=1 {"id":2,"x":2} id=2 arr2: {"id":3,"x":3} id=3 Hash Map Structure Key | Value 1 | {id:1,x:1} 2 | {id:2,x:2} 3 | {id:3,x:3} (from arr2) ALGORITHM STEPS 1 Initialize Hash Map Create empty map for results 2 Process arr1 Add all objects by id key 3 Process arr2 Merge or add, arr2 overrides 4 Sort by ID Return sorted array One Pass Processing for obj in arr1: map[obj.id] = obj for obj in arr2: map[obj.id] = merge() FINAL RESULT Joined Array (sorted by id) {"id": 1, "x": 1} from arr1 {"id": 2, "x": 2} from arr1 {"id": 3, "x": 3} from arr2 Output: [{id:1,x:1},{id:2,x:2},{id:3,x:3}] OK - 3 items Key Insight: Using a Hash Map with id as key allows O(n+m) time complexity for merging. Process arr1 first, then arr2 - this ensures arr2 values override arr1 when ids match. Final sort by id ensures ascending order. Space: O(n+m) for the hash map storage. TutorialsPoint - Join Two Arrays by ID | One Pass Hash Map Approach
Asked in
Google 32 Amazon 28 Facebook 25 Microsoft 22
23.4K Views
High Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen