Skip to content

CodeNot attempted

chunk

Difficulty: easy

Split an array into groups of size (default 1). The last group holds the remainder. Don't mutate the input.

Show reference solution
export function chunk<T>(arr: readonly T[], size = 1): T[][] {
if (size < 1) {
return [];
}
const result: T[][] = [];
let temp: T[] = [];
for (const item of arr) {
temp.push(item);
if (temp.length === size) {
result.push(temp);
temp = [];
}
}
if (temp.length > 0) {
result.push(temp); // the last, shorter chunk
}
return result;
}
Loading editor…