chunkArrayInGroups

Easy · Array Manipulations · 2 test cases

Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.

Examples

chunkArrayInGroups(['a', 'b', 'c', 'd'], 2) → [['a', 'b'], ['c', 'd']]

Starter code

function chunkArrayInGroups(arr, size) {

}

Complexity of the optimal solution

Time O(n), space O(n). The function iterates through the original array. The `slice` operation inside the loop creates new sub-arrays. Since every element from the original array is copied exactly once into the new structure, both time and space complexity are O(n).

Solve chunkArrayInGroups in the browser