{ "data": { "question": { "questionId": "2858", "questionFrontendId": "2722", "boundTopicId": null, "title": "Join Two Arrays by ID", "titleSlug": "join-two-arrays-by-id", "content": "

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.

\n\n

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.

\n\n

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

\n\n\n\n

 

\n

Example 1:

\n\n
\nInput: \narr1 = [\n    {"id": 1, "x": 1},\n    {"id": 2, "x": 9}\n], \narr2 = [\n    {"id": 3, "x": 5}\n]\nOutput: \n[\n    {"id": 1, "x": 1},\n    {"id": 2, "x": 9},\n    {"id": 3, "x": 5}\n]\nExplanation: There are no duplicate ids so arr1 is simply concatenated with arr2.\n
\n\n

Example 2:

\n\n
\nInput: \narr1 = [\n    {"id": 1, "x": 2, "y": 3},\n    {"id": 2, "x": 3, "y": 6}\n], \narr2 = [\n    {"id": 2, "x": 10, "y": 20},\n    {"id": 3, "x": 0, "y": 0}\n]\nOutput: \n[\n    {"id": 1, "x": 2, "y": 3},\n    {"id": 2, "x": 10, "y": 20},\n    {"id": 3, "x": 0, "y": 0}\n]\nExplanation: 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.\n
\n\n

Example 3:

\n\n
\nInput: \narr1 = [\n    {"id": 1, "b": {"b": 94},"v": [4, 3], "y": 48}\n]\narr2 = [\n    {"id": 1, "b": {"c": 84}, "v": [1, 3]}\n]\nOutput: [\n    {"id": 1, "b": {"c": 84}, "v": [1, 3], "y": 48}\n]\nExplanation: 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.
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 13, "dislikes": 3, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "[{\"id\": 1,\"x\": 1},{\"id\": 2,\"x\": 9}]\n[{\"id\": 3,\"x\": 5}]\n[{\"id\": 1,\"x\": 2,\"y\": 3},{\"id\": 2,\"x\": 3,\"y\": 6}]\n[{\"id\": 2,\"x\": 10,\"y\": 20},{\"id\": 3,\"x\": 0,\"y\": 0}]\n[{\"id\":1,\"b\":{\"b\": 94},\"v\":[4,3],\"y\":48}]\n[{\"id\":1,\"b\":{\"c\": 84},\"v\":[1,3]}]", "categoryTitle": "JavaScript", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {Array} arr1\n * @param {Array} arr2\n * @return {Array}\n */\nvar join = function(arr1, arr2) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "function join(arr1: any[], arr2: any[]): any[] {\n\n};", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"571\", \"totalSubmission\": \"880\", \"totalAcceptedRaw\": 571, \"totalSubmissionRaw\": 880, \"acRate\": \"64.9%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "[{\"id\": 1,\"x\": 1},{\"id\": 2,\"x\": 9}]\n[{\"id\": 3,\"x\": 5}]", "metaData": "{\n \"name\": \"join\",\n \"params\": [\n {\n \"name\": \"arr1\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"arr2\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"typescript\": [\"Typescript\", \"

TypeScript 4.5.4, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2020 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }