Given an array arr and a chunk size size, return a chunked array.
A chunked array contains the original elements in arr, but consists of subarrays each of length size. The length of the last subarray may be less than size if arr.length is not evenly divisible by size.
You may assume the array is the output of JSON.parse. In other words, it is valid JSON.
Please solve it without using lodash's _.chunk function.
Example 1:
Input: arr = [1,2,3,4,5], size = 1 Output: [[1],[2],[3],[4],[5]] Explanation: The arr has been split into subarrays each with 1 element.
Example 2:
Input: arr = [1,9,6,3,2], size = 3 Output: [[1,9,6],[3,2]] Explanation: The arr has been split into subarrays with 3 elements. However, only two elements are left for the 2nd subarray.
Example 3:
Input: arr = [8,5,3,2,6], size = 6 Output: [[8,5,3,2,6]] Explanation: Size is greater than arr.length thus all elements are in the first subarray.
Example 4:
Input: arr = [], size = 1 Output: [] Explanation: There are no elements to be chunked so an empty array is returned.
Constraints:
arr is a valid JSON array2 <= JSON.stringify(arr).length <= 1051 <= size <= arr.length + 1Problem Overview: You’re given an array arr and a chunk size size. The goal is to split the array into smaller arrays where each chunk contains at most size elements. If the final group has fewer elements than size, it should still be included as the last chunk.
Approach 1: Iterative Slicing (Time: O(n), Space: O(n))
The most direct solution walks through the array in steps of size and extracts subarrays using slicing. Start at index 0, take a slice from i to i + size, append it to the result, then move forward by size. This continues until the end of the array. Because every element is copied into exactly one chunk, the algorithm runs in O(n) time. The output array stores all elements again in nested form, so the extra space usage is also O(n).
This approach is easy to reason about and maps directly to how the problem is described. Languages like Python and JavaScript make it especially clean because slicing operations are built in. If you’re comfortable with basic array traversal and iteration, this is usually the first solution you’d implement in an interview or production code.
Approach 2: Using Modulo and Remainder Logic (Time: O(n), Space: O(n))
Another method processes the array one element at a time while dynamically building the current chunk. Maintain a temporary list representing the current group. As you iterate through the array, push each element into this list. When the index satisfies i % size == 0 (or when the current chunk reaches the desired length), start a new chunk and append the previous one to the result.
The key idea relies on modulo arithmetic to determine chunk boundaries. Every size elements, a new group begins. This avoids slicing operations and instead builds chunks incrementally. Time complexity remains O(n) because each element is processed once. Space complexity is O(n) due to storing the resulting chunked structure.
This version can be useful in languages where slicing is expensive or unavailable. It also provides more control if chunk construction involves additional logic such as filtering, transformation, or streaming data processing.
Recommended for interviews: Interviewers generally expect the iterative slicing solution because it’s concise and clearly communicates intent. Starting with the straightforward iteration approach demonstrates strong understanding of array traversal. Mentioning the modulo-based grouping afterward shows deeper awareness of alternative implementations and edge-case handling.
This approach involves iterating over the array using a loop and extracting subarrays using slicing. The loop increments by the chunk size in each iteration, thus effectively slicing the array into chunks of the desired size.
The chunk_array function uses list comprehension which iterates over the array with a step size of size. It slices arr from index i to i + size in each iteration, creating subarrays of the desired chunk size.
Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(n), storing the entire chunked array requires space proportional to the input size.
This approach uses simple arithmetic operations to determine when to create a new subarray. Using the modulus operator allows checking if the number of currently collected elements is equal to the chunk size, upon which a new subarray is started.
The method keeps appending elements to the current chunk list until its length reaches size, at which point the chunk is added to the chunked result list. If there is a leftover partial chunk at the end, it is also added to the result.
Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(n), because the additional space needed is proportional to the input size.
TypeScript
JavaScript
| Approach | Complexity |
|---|---|
| Iterative Slicing | Time Complexity: O(n), where n is the number of elements in the array. |
| Using Modulo and Remainder Logic | Time Complexity: O(n), where n is the number of elements in the array. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Slicing | O(n) | O(n) | Best for readability and languages with built-in slicing like Python or JavaScript |
| Modulo and Remainder Logic | O(n) | O(n) | When building chunks incrementally or when slicing operations are not ideal |
Chunk Array - Leetcode 2677 - JavaScript 30-Day Challenge • NeetCodeIO • 5,145 views views
Watch 9 more video solutions →Practice Chunk Array with our built-in code editor and test cases.
Practice on FleetCode