2631. Group By LeetCode Solution
In this guide, you will get 2631. Group By LeetCode Solution with the best time and space complexity. The solution to Group By problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Group By solution in C++
- Group By solution in Java
- Group By solution in Python
- Additional Resources

Problem Statement of 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 is the output of fn(arr[i]) and each value is an array containing all items in the original array which generate 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
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
2631. Group By LeetCode Solution in C++
declare global {
interface Array<T> {
groupBy(fn: (item: T) => string): Record<string, T[]>;
}
}
Array.prototype.groupBy = function (fn) {
const ans: Record<string, any[]> = {};
for (const item of this) {
const key = fn(item);
if (ans[key] === undefined) {
ans[key] = [];
}
ans[key].push(item);
}
return ans;
};
/* code provided by PROGIEZ */
2631. Group By LeetCode Solution in Java
N/A
// code provided by PROGIEZ
2631. Group By LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.