## DEV Community is a community of 750,997 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Solving the Array Chunking Problem

I have been enjoying learning more about data structures and algorithms lately. Early on, however, I was overwhelmed by all of the different types of problems and patterns one needed to become familiar with. This array chunking problem is one of the first problems where I felt I was starting to get a grasp on the process and applying techniques from previous problems. I encountered it in Stephen Grider's Coding Interview Bootcamp on Udemy, which I highly recommend.

The Problem
You are given an array and size and are asked to divide the array into smaller subarrays of length equal to the given size.

Ex.

``````chunk([1, 2, 3, 4, 5, 6], 2)
//Output will be [[1, 2], [3, 4], [5, 6]]

chunk([1, 2, 3, 4, 5], 3)
//Output will be [[1, 2, 3], [4, 5]]

// chunk([1, 2, 3, 4, 5], 2)
//Output will be [[ 1, 2], [3, 4], ]
``````

One detail I point out is that when they give us the size, it's the desired length of the subarrays, NOT the number of arrays they want us to create. It's details like this one that I find valuable when I'm reading through the problem. It also helps to have sample cases for clarification.

Solution

The first step is to identify what exactly the problem is asking for, which in this case is an array comprised of smaller arrays. That being said, one approach we can take is to create our empty array that will eventually hold all of the subarrays.

``````function chunk(array, size) {
const chunkedArray = [];

}
``````

Great, now that it's set up, we will write out a loop to perform work on the original array. First we'll set up a counter that I will give the variable name of `index`.

``````function chunk(array, size) {
const chunkedArray = [];

let index = 0;
}
``````

Next we set up a while loop, that says while the index is less than the length of our given array, we will perform some type of work.

``````function chunk(array, size) {
const chunkedArray = [];

let index = 0;

while (index < array.length) {
//Only the first line of the while loop

}
``````

Following that is the actual work to begin chunking the first array into subarrays. Using the `.slice()` method, we can cut out sections with lengths equal to the given size, from the original array.

``````array.slice(index, index + size)
``````

The `.slice()` method will start at the first argument (start Index) you pass in, and will stop slicing at the second (optional) argument you pass in, and will not include the end(end Index). Just a reminder that if you don't pass in a second argument to slice, it will start at the first argument and slice to the end of the array.

`.slice(startIndex, endIndex)`

Let's look at what we currently have in our code.

``````function chunk(array, size) {
const chunkedArray = [];

let index = 0;

while (index < array.length) {
array.slice(index, index + size);
index += size;
}

}
``````

Here we are slicing the array from the index, up to index + size, then we are incrementing the index by the value of size, in order to move on to the next "chunk" of the array.

This is good so far, but so far, those new "chunks" don't have anywhere to live yet. This is where we come back to the empty array we declared in our first step. This currently empty array is going to contain the new chunks as they are created. We can use the `.push()` method to move those arrays chunks into our empty array.

``````function chunk(array, size) {
const chunkedArray = [];

let index = 0;

while (index < array.length) {
chunkedArray.push(array.slice(index, index + size));
index += size;
}

}
``````

Now as our while loop iterates through the original array, it is pushing chunks created with the slice method into the chunkedArray.

Lastly, we have to ensure that our function is returning what the problem requested, in this case, an array containing smaller subarrays.

So we simply return the chunkedArray outside of the while loop, because we don't want to return a chunkedArray for every iteration, just the final array.

``````function chunk(array, size) {
const chunkedArray = [];

let index = 0;

while (index < array.length) {
chunkedArray.push(array.slice(index, index + size));
index += size;
}
return chunkedArray;
}
``````

That will give you the array comprised of subarrays as your answer. This problem was helpful in my studies as it gave me more practice with the while loop and implemented the useful slice method.

Resources

Stephen Grider's Algorithms and Data Structures Udemy Course

Interview Cake