1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/originData/join-two-arrays-by-id.json

59 lines
12 KiB
JSON
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"data": {
"question": {
"questionId": "2858",
"questionFrontendId": "2722",
"categoryTitle": "JavaScript",
"boundTopicId": 2300585,
"title": "Join Two Arrays by ID",
"titleSlug": "join-two-arrays-by-id",
"content": "<p>Given two arrays <code>arr1</code> and <code>arr2</code>, return a new&nbsp;array <code>joinedArray</code>. All the objects in each&nbsp;of the two inputs arrays will contain an&nbsp;<code>id</code>&nbsp;field that has an integer value.&nbsp;<code>joinedArray</code>&nbsp;is an array formed by merging&nbsp;<code>arr1</code> and <code>arr2</code> based on&nbsp;their <code>id</code>&nbsp;key. The length of&nbsp;<code>joinedArray</code> should be the length of unique values of <code>id</code>. The returned array should be sorted in&nbsp;<strong>ascending</strong>&nbsp;order based on the <code>id</code>&nbsp;key.</p>\n\n<p>If a given&nbsp;<code>id</code>&nbsp;exists in one array but not the other, the single object with that&nbsp;<code>id</code> should be included in the result array without modification.</p>\n\n<p>If two objects share an <code>id</code>, their properties should be merged into a single&nbsp;object:</p>\n\n<ul>\n\t<li>If a key only exists in one object, that single key-value pair should be included in the object.</li>\n\t<li>If a key is included in both objects, the value in the object from <code>arr2</code>&nbsp;should override the value from <code>arr1</code>.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr1 = [\n&nbsp; {&quot;id&quot;: 1, &quot;x&quot;: 1},\n&nbsp; {&quot;id&quot;: 2, &quot;x&quot;: 9}\n], \narr2 = [\n {&quot;id&quot;: 3, &quot;x&quot;: 5}\n]\n<strong>Output:</strong> \n[\n&nbsp; {&quot;id&quot;: 1, &quot;x&quot;: 1},\n&nbsp; {&quot;id&quot;: 2, &quot;x&quot;: 9},\n {&quot;id&quot;: 3, &quot;x&quot;: 5}\n]\n<strong>Explanation:</strong> There are no duplicate ids so arr1 is simply concatenated with arr2.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr1 = [\n {&quot;id&quot;: 1, &quot;x&quot;: 2, &quot;y&quot;: 3},\n {&quot;id&quot;: 2, &quot;x&quot;: 3, &quot;y&quot;: 6}\n], \narr2 = [\n {&quot;id&quot;: 2, &quot;x&quot;: 10, &quot;y&quot;: 20},\n {&quot;id&quot;: 3, &quot;x&quot;: 0, &quot;y&quot;: 0}\n]\n<strong>Output:</strong> \n[\n {&quot;id&quot;: 1, &quot;x&quot;: 2, &quot;y&quot;: 3},\n {&quot;id&quot;: 2, &quot;x&quot;: 10, &quot;y&quot;: 20},\n&nbsp; {&quot;id&quot;: 3, &quot;x&quot;: 0, &quot;y&quot;: 0}\n]\n<strong>Explanation:</strong> 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</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr1 = [\n {&quot;id&quot;: 1, &quot;b&quot;: {&quot;b&quot;: 94},&quot;v&quot;: [4, 3], &quot;y&quot;: 48}\n]\narr2 = [\n {&quot;id&quot;: 1, &quot;b&quot;: {&quot;c&quot;: 84}, &quot;v&quot;: [1, 3]}\n]\n<strong>Output:</strong> [\n {&quot;id&quot;: 1, &quot;b&quot;: {&quot;c&quot;: 84}, &quot;v&quot;: [1, 3], &quot;y&quot;: 48}\n]\n<strong>Explanation:</strong> The two objects with id=1 are merged together. For the keys &quot;b&quot; and &quot;v&quot; the values from arr2 are used. Since the key &quot;y&quot; only exists in arr1, that value is taken form arr1.</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>arr1</code> and <code>arr2</code> are valid JSON arrays</li>\n\t<li>Each object in <code>arr1</code> and <code>arr2</code> has a unique&nbsp;integer <code>id</code> key</li>\n\t<li><code>2 &lt;= JSON.stringify(arr1).length &lt;= 10<sup>6</sup></code></li>\n\t<li><code>2 &lt;= JSON.stringify(arr2).length &lt;= 10<sup>6</sup></code></li>\n</ul>\n",
"translatedTitle": "根据 ID 合并两个数组",
"translatedContent": "<p>现给定两个数组 <code>arr1</code> 和 <code>arr2</code> ,返回一个新的数组 <code>joinedArray</code> 。两个输入数组中的每个对象都包含一个 <code>id</code> 字段。<code>joinedArray</code> 是一个通过 <code>id</code> 将 <code>arr1</code> 和 <code>arr2</code> 连接而成的数组。<code>joinedArray</code> 的长度应为唯一值 <code>id</code> 的长度。返回的数组应按 <code>id</code> <strong>升序</strong> 排序。</p>\n\n<p>如果一个 <code>id</code> 存在于一个数组中但不存在于另一个数组中,则该对象应包含在结果数组中且不进行修改。</p>\n\n<p>如果两个对象共享一个 <code>id</code> ,则它们的属性应进行合并:</p>\n\n<ul>\n\t<li>如果一个键只存在于一个对象中,则该键值对应该包含在对象中。</li>\n\t<li>如果一个键在两个对象中都包含,则 <code>arr2</code> 中的值应覆盖 <code>arr1</code> 中的值。</li>\n</ul>\n\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例 1</strong></p>\n\n<pre>\n<b>输入:</b>\narr1 = [\n&nbsp; {\"id\": 1, \"x\": 1},\n&nbsp; {\"id\": 2, \"x\": 9}\n], \narr2 = [\n {\"id\": 3, \"x\": 5}\n]\n<b>输出:</b>\n[\n&nbsp; {\"id\": 1, \"x\": 1},\n&nbsp; {\"id\": 2, \"x\": 9},\n {\"id\": 3, \"x\": 5}\n]\n<b>解释:</b>没有共同的 id因此将 arr1 与 arr2 简单地连接起来。\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>\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]\n<b>输出:</b>\n[\n {\"id\": 1, \"x\": 2, \"y\": 3},\n {\"id\": 2, \"x\": 10, \"y\": 20},\n&nbsp; {\"id\": 3, \"x\": 0, \"y\": 0}\n]\n<b>解释:</b>id 为 1 和 id 为 3 的对象在结果数组中保持不变。id 为 2 的两个对象合并在一起。arr2 中的键覆盖 arr1 中的值。\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<b>输入:</b>\narr1 = [\n {\"id\": 1, \"b\": {\"b\": 94},\"v\": [4, 3], \"y\": 48}\n]\narr2 = [\n {\"id\": 1, \"b\": {\"c\": 84}, \"v\": [1, 3]}\n]\n<strong>输出:</strong> [\n {\"id\": 1, \"b\": {\"c\": 84}, \"v\": [1, 3], \"y\": 48}\n]\n<b>解释:</b>具有 id 为 1 的对象合并在一起。对于键 \"b\" 和 \"v\" ,使用 arr2 中的值。由于键 \"y\" 只存在于 arr1 中,因此取 arr1 的值。</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>arr1 和 arr2 都是有效的 JSON 数组</code></li>\n\t<li><code>在 arr1 和 arr2 中都有唯一的键值 id</code></li>\n\t<li><code>2 &lt;= JSON.stringify(arr1).length &lt;= 10<sup>6</sup></code></li>\n\t<li><code>2 &lt;= JSON.stringify(arr2).length &lt;= 10<sup>6</sup></code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 4,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}",
"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": "type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };\n\nfunction join(arr1: JSONValue[], arr2: JSONValue[]): JSONValue[] {\n\t\n};",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"2.5K\", \"totalSubmission\": \"5.1K\", \"totalAcceptedRaw\": 2450, \"totalSubmissionRaw\": 5122, \"acRate\": \"47.8%\"}",
"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,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 5.1.6<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"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]}]",
"__typename": "QuestionNode"
}
}
}