Skip to content
Onsite

Chunk Array

EasyP2Coding

Implement chunk(array, size), which splits an array into consecutive pieces of length size and returns them as an array of arrays.

The last piece holds the remainder and may be shorter. An empty array returns []. size is always a positive integer, possibly larger than the array. The input is not modified.

Example 1

Input
chunk([1, 2, 3, 4, 5], 2)
Output
[[1, 2], [3, 4], [5]]

Example 2

Input
chunk([1, 2, 3], 5)
Output
[[1, 2, 3]]

Constraints

  • 0 <= array.length <= 10^5
  • size >= 1

Hints

Run checks the 3 visible tests, Submit all 8.