Group By — LeetCode 2631 Python Solution

MediumJavaScript
Problem
#2631
Reading time
2 min

The problem

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.

Example

Input
array = [
Output
{

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1) to O(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2631. Group By?
LeetCode 2631. Group By is rated Medium on LeetCode.
What topics does LeetCode 2631. Group By cover?
LeetCode 2631. Group By is tagged JavaScript on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview