게으른개발너D
[Array, prototype] Group By ⭐️ 본문
https://leetcode.com/problems/group-by/description/
Group By - LeetCode
Can you solve this real interview question? Group By - Write code that enhances all arrays such that you can call the array.groupBy(fn) method on any array and it will return a grouped version of the array. A grouped array is an object where each key
leetcode.com
Write code that enhances all arrays such that you can call the array.groupBy(fn) method on any array and it will return a grouped version of the array.
A grouped array is an object where each key is the output of fn(arr[i]) and each value is an array containing all items in the original array with that key.
The provided callback fn will accept an item in the array and return a string key.
The order of each value list should be the order the items appear in the array. Any order of keys is acceptable.
Please solve it without lodash's _.groupBy function.
Example 1:
Input:
array = [
{"id":"1"},
{"id":"1"},
{"id":"2"}
],
fn = function (item) {
return item.id;
}
Output:
{
"1": [{"id": "1"}, {"id": "1"}],
"2": [{"id": "2"}]
}
Explanation:
Output is from array.groupBy(fn).
The selector function gets the "id" out of each item in the array.
There are two objects with an "id" of 1. Both of those objects are put in the first array.
There is one object with an "id" of 2. That object is put in the second array.
Example 2:
Input:
array = [
[1, 2, 3],
[1, 3, 5],
[1, 5, 9]
]
fn = function (list) {
return String(list[0]);
}
Output:
{
"1": [[1, 2, 3], [1, 3, 5], [1, 5, 9]]
}
Explanation:
The array can be of any type. In this case, the selector function defines the key as being the first element in the array.
All the arrays have 1 as their first element so they are grouped together.
{
"1": [[1, 2, 3], [1, 3, 5], [1, 5, 9]]
}
Example 3:
Input:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
fn = function (n) {
return String(n > 5);
}
Output:
{
"true": [6, 7, 8, 9, 10],
"false": [1, 2, 3, 4, 5]
}
Explanation:
The selector function splits the array by whether each number is greater than 5.
Constraints:
- 0 <= array.length <= 105
- fn returns a string
solution
/**
* @param {Function} fn
* @return {Array}
*/
Array.prototype.groupBy = function(fn) {
const answer = {};
for(const value of this) {
const key = fn(value);
answer[key] = answer[key] || [];
answer[key].push(value);
}
return answer;
};
/**
* [1,2,3].groupBy(String) // {"1":[1],"2":[2],"3":[3]}
*/
solution2) reduce를 이용한 풀이
/**
* @param {*} fn
* @returns
*/
Array.prototype.groupBy = function(fn) {
return this.reduce((grouped, item) => {
const key = fn(item);
if(!grouped[key]) {
grouped[key] = [];
}
grouped[key].push(item);
return grouped;
}, {});
};
const array = [1, 2, 3, 4, 5];
array.reduce();
이런식으로 앞쪽에 변수를 붙여서 사용하는 메소드를 정의할때는, 함수한에 해당 변수인 array를 this라고 부른다.
'알고리즘 > 과제' 카테고리의 다른 글
[Function, prototype] Call Function with Custom Context (prototype 함수 안에 prototype 정의하기) ⭐️ (0) | 2023.07.04 |
---|---|
[Generator, Array] Nested Array Generator (feat. flat) (0) | 2023.07.04 |
[Function] Debounce ⭐️ (0) | 2023.07.04 |
[Class] Calculator with Method Chaining (method에 return 값) ⭐️ (0) | 2023.07.04 |
[Promise] Add Two Promises (feat. all) ⭐️ (0) | 2023.07.04 |