Chunk Array
EasyData EngineeringArray
Description
Given an array and a positive batch size k, split the array into consecutive chunks of size k. The final chunk holds the remaining elements when the length is not divisible by k.
Examples
Input:
[1,2,3,4,5], 2Output:
[[1,2],[3,4],[5]]Explanation:
The array is sliced into consecutive blocks of the given size, with the final block holding whatever remains.
Input:
[1,2,3,4], 2Output:
[[1,2],[3,4]]Explanation:
The array is sliced into consecutive blocks of the given size, with the final block holding whatever remains.
Input:
[1,2,3], 1Output:
[[1],[2],[3]]Explanation:
The array is sliced into consecutive blocks of the given size, with the final block holding whatever remains.
Constraints
- •
1 ≤ array length ≤ 10⁴ - •
1 ≤ k ≤ array length