Chunk Array | #2677 | LeetCode Solution

Author: neptune | 19th-Sep-2023

Problem : Chunk Array | #2677 | LeetCode

Given an array arr and a chunk `size`, return a `chunked` array. A `chunked` array contains the original elements in `arr`, but consists of subarrays each of length `size`. The length of the last subarray may be less than `size` if `arr.length` is not evenly divisible by `size`.

You may assume the array is the output of `JSON.parse`. In other words, it is valid JSON.

Please solve it without using lodash's `_.chunk` function.


Example 1:

Input: arr = [1,2,3,4,5], size = 1

Output: [[1],[2],[3],[4],[5]]

Explanation: The arr has been split into subarrays each with 1 element.

Example 2:

Input: arr = [1,9,6,3,2], size = 3

Output: [[1,9,6],[3,2]]

Explanation: The arr has been split into subarrays with 3 elements. However, only two elements are left for the 2nd subarray.


Example 3:

Input: arr = [8,5,3,2,6], size = 6

Output: [[8,5,3,2,6]]

Explanation: Size is greater than arr.length thus all elements are in the first subarray.

Example 4:

Input: arr = [], size = 1

Output: []

Explanation: There are no elements to be chunked so an empty array is returned.


Solution:

    /**

     * @param {Array} arr

     * @param {number} size

     * @return {Array}

     */

    var chunk = function(arr, size) {

        const chunkarr = []

        for(let i = 0; i<arr.length; i+=size){

            var subarr = arr.slice(i, i+size);

            chunkarr.push(subarr);

        }

        return chunkarr;

    };



Explanation:

Let's break down the code step by step:


1. Initialize an empty array `chunkedArray` to store the chunked subarrays.


2. Use a `for` loop to iterate through the input array `arr`. The loop variable `i` represents the starting index of each chunk.


3. Inside the loop, use the `slice` method to extract a chunk of `size` elements from the original array. `arr.slice(i, i + size)` extracts a subarray starting from index `i` (inclusive) and ending at index `i + size` (exclusive).


4. Add the chunked subarray to the `chunkedArray` using the `push` method.


5. Repeat steps 3 and 4 until the entire input array has been processed.


6. Finally, return the `chunkedArray` containing the subarrays of the specified size.


The code creates subarrays of the desired size from the input array `arr` and returns a new array containing these subarrays.





👉 Read More
Generate Fibonacci Sequence - JavaScript | Hackerank
Managing Virtual Environments in React JavaScript Projects
To Be Or Not To Be | #2704 | LeetCode Solution
Apply Transform Over Each Element in Array | #2635 | LeetCode Solution
Function Composition | #2629 | LeetCode Solution
Counter | #2620 | LeetCode Solution
Different ways to handle state in React applications
Counter 2 | #2665 | LeetCode Solution
Array Reduce Transformation | #2626 | LeetCode Solution
Add Two Promises | #2723 | LeetCode Solution
Filter Elements from Array | #2634 | LeetCode Solution
Arrow Functions in JavaScript | ES6
From REST to GraphQL: The Future of API Design
Is Object Empty | #2727 | LeetCode | JavaScript Solution
How I Built My Blogging Website Using React, Node.js, and Jamstack Architecture?
How to Perform Unit Testing in React Components with Examples?
Do you know ! How to manage State in Functional & Class Components in React ?
A Guide to Writing Clean, Readable, and Maintainable Code in JavaScript
How to Get Started with Jamstack: A Comprehensive Guide?
Why, What, and When: Understanding Jamstack?
Explore more Blogs...