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/originData/group-by.json
2023-12-09 19:57:46 +08:00

67 lines
8.1 KiB
JSON

{
"data": {
"question": {
"questionId": "2742",
"questionFrontendId": "2631",
"boundTopicId": null,
"title": "Group By",
"titleSlug": "group-by",
"content": "<p>Write code that enhances all arrays such that you can call the&nbsp;<code>array.groupBy(fn)</code>&nbsp;method on any array and it will return a <strong>grouped</strong>&nbsp;version of the array.</p>\n\n<p>A&nbsp;<strong>grouped</strong>&nbsp;array is an object where each&nbsp;key&nbsp;is&nbsp;the output of&nbsp;<code>fn(arr[i])</code>&nbsp;and each&nbsp;value is an array containing all items in the original array with that key.</p>\n\n<p>The provided callback&nbsp;<code>fn</code>&nbsp;will accept an item in the array and return a string key.</p>\n\n<p>The order of each value list should be the order the items&nbsp;appear in the array. Any order of keys is acceptable.</p>\n\n<p>Please solve it without lodash&#39;s&nbsp;<code>_.groupBy</code> function.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narray = [\n&nbsp; {&quot;id&quot;:&quot;1&quot;},\n&nbsp; {&quot;id&quot;:&quot;1&quot;},\n&nbsp; {&quot;id&quot;:&quot;2&quot;}\n], \nfn = function (item) { \n&nbsp; return item.id; \n}\n<strong>Output:</strong> \n{ \n&nbsp; &quot;1&quot;: [{&quot;id&quot;: &quot;1&quot;}, {&quot;id&quot;: &quot;1&quot;}], &nbsp; \n&nbsp; &quot;2&quot;: [{&quot;id&quot;: &quot;2&quot;}] \n}\n<strong>Explanation:</strong>\nOutput is from array.groupBy(fn).\nThe selector function gets the &quot;id&quot; out of each item in the array.\nThere are two objects with an &quot;id&quot; of 1. Both of those objects are put in the first array.\nThere is one object with an &quot;id&quot; of 2. That object is put in the second array.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narray = [\n&nbsp; [1, 2, 3],\n&nbsp; [1, 3, 5],\n&nbsp; [1, 5, 9]\n]\nfn = function (list) { \n&nbsp; return String(list[0]); \n}\n<strong>Output:</strong> \n{ \n&nbsp; &quot;1&quot;: [[1, 2, 3], [1, 3, 5], [1, 5, 9]] \n}\n<strong>Explanation:</strong>\nThe array can be of any type. In this case, the selector function defines the key as being the first element in the array. \nAll the arrays have 1 as their first element so they are grouped together.\n{\n &quot;1&quot;: [[1, 2, 3], [1, 3, 5], [1, 5, 9]]\n}\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nfn = function (n) { \n&nbsp; return String(n &gt; 5);\n}\n<strong>Output:</strong>\n{\n&nbsp; &quot;true&quot;: [6, 7, 8, 9, 10],\n&nbsp; &quot;false&quot;: [1, 2, 3, 4, 5]\n}\n<strong>Explanation:</strong>\nThe selector function splits the array by whether each number is greater than 5.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 &lt;= array.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>fn</code> returns a string</li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 252,
"dislikes": 10,
"isLiked": null,
"similarQuestions": "[{\"title\": \"Filter Elements from Array\", \"titleSlug\": \"filter-elements-from-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Apply Transform Over Each Element in Array\", \"titleSlug\": \"apply-transform-over-each-element-in-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Snail Traversal\", \"titleSlug\": \"snail-traversal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Array Reduce Transformation\", \"titleSlug\": \"array-reduce-transformation\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Array Upper Bound\", \"titleSlug\": \"array-upper-bound\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
"exampleTestcases": "[{\"id\":\"1\"},{\"id\":\"1\"},{\"id\":\"2\"}]\nfunction (item) { return item.id; }\n[[1,2,3],[1,3,5],[1,5,9]]\nfunction (list) { return String(list[0]); }\n[1,2,3,4,5,6,7,8,9,10]\nfunction (n) { return String(n > 5); }",
"categoryTitle": "JavaScript",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Function} fn\n * @return {Object}\n */\nArray.prototype.groupBy = function(fn) {\n \n};\n\n/**\n * [1,2,3].groupBy(String) // {\"1\":[1],\"2\":[2],\"3\":[3]}\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "declare global {\n interface Array<T> {\n groupBy(fn: (item: T) => string): Record<string, T[]>\n }\n}\n\nArray.prototype.groupBy = function(fn) {\n \n}\n\n/**\n * [1,2,3].groupBy(String) // {\"1\":[1],\"2\":[2],\"3\":[3]}\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"22.7K\", \"totalSubmission\": \"27.6K\", \"totalAcceptedRaw\": 22690, \"totalSubmissionRaw\": 27641, \"acRate\": \"82.1%\"}",
"hints": [
"First declare an object that will eventually be returned.",
"Iterate of each element in the array. You can access the array with the \"this\" keyword.",
"The key is fn(arr[i]). If the key already exists on the object, set the value to be an empty array. Then push the value onto the array at the key."
],
"solution": {
"id": "1918",
"canSeeDetail": false,
"paidOnly": true,
"hasVideoSolution": false,
"paidOnlyVideo": true,
"__typename": "ArticleNode"
},
"status": null,
"sampleTestCase": "[{\"id\":\"1\"},{\"id\":\"1\"},{\"id\":\"2\"}]\nfunction (item) { return item.id; }",
"metaData": "{\n \"name\": \"groupBy\",\n \"params\": [\n {\n \"name\": \"list\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\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\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 5.1.6, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2022 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}