Chunk Array

Arrays

Write chunk(arr, size) splitting the array into subarrays of length size.

The final group holds the remainder and may be shorter. A size of zero or less cannot produce groups — return an empty array.

Examples

Example 1
Input
chunk([1, 2, 3, 4, 5], 2);
Output
[[1, 2], [3, 4], [5]]
Explanation
Two full groups and a remainder of one.

Constraints

  • 0 <= arr.length <= 10^5
  • Any integer size

Notes

  • `slice` past the end of an array simply stops, so the short final group needs no special case.

Hints

Read the full write-up for Chunk Array
</>JavaScript
Loading editor…
Test Result
Run your code, or Submit to test it