---
title: "Chunk Arrays"  
description: "Chunk Arrays"  
author: "Steilla Mitchel"  
published: 2024-06-12  
updated: 2024-06-12  
canonical: https://www.mindstick.com/forum/160732/chunk-arrays  
category: "c#"  
tags: ["c#", "programming language", "programming help", "programs"]  
reading_time: 2 minutes  

---

# Chunk Arrays

Given an array `arr` and a chunk [size](https://www.mindstick.com/forum/159799/how-can-you-manage-the-size-of-mdf-and-ldf-files) `size`, return a **chunked** array.

A **chunked** [array contains](https://www.mindstick.com/forum/23089/in-java-how-can-i-test-if-an-array-contains-a-certain-value) the original [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements) in `arr`, but consists of subarrays each of [length](https://www.mindstick.com/interview/1915/what-is-the-difference-between-char_length-and-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](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) of `JSON.parse`. In other words, it is valid JSON.

Please solve it without using lodash's `_.chunk` function.

## Replies

### Reply by Ravi Vishwakarma

let's write code in the `C#` language to solve the Chunks [Array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) problem.

```cs
// Method to divide the array into chunks of the specified size
public static int[][] ChunksArray(int[] array, int size)
{
    // Initialize a list to hold the chunks of arrays
    IList<int[]> list = new List<int[]>();

    // Return an empty array of arrays if the input array is empty
    if (array.Length == 0)
    {
        return list.ToArray();
    }

    // Iterate through the array, creating chunks of the specified size
    for (int i = 0; i < array.Length; i += size)
    {
        // Calculate the size of the current chunk
        int chunkSize = Math.Min(size, array.Length - i);
        // Create a new chunk and copy elements from the original array
        int[] chunk = new int[chunkSize];
        Array.Copy(array, i, chunk, 0, chunkSize);

        // Add the chunk to the list
        list.Add(chunk);
    }

    // Convert the list of chunks to an array of arrays and return it
    return list.ToArray();
}
```

Now call this method from the Program class.

```cs
    public class Program
    {
        public static void Main()
        {
            IList<int[]> list = new List<int[]>
            {
                new int[] { 1, 2, 3, 4, 5 },
                new int[] { 1, 9, 6, 3, 2 },
                new int[] { 8, 5, 3, 2, 6 },
                new int[]{ }
            };

            foreach (var item in list)
            {
                var data = Program.ChunksArray(item, new Random().Next(1, 6));
                Console.Write("[");
                foreach (var rows in data)
                {
                    Console.Write("[");
                    Console.Write(string.Join(", ", rows));
                    Console.Write("],");
                }
                Console.WriteLine("]");
            }

            Console.ReadLine();
        }
    }
```

## Output :

```plaintext
[[1, 2, 3, 4, 5],]
[[1, 9, 6, 3, 2],]
[[8, 5],[3, 2],[6],]
[]
```


---

Original Source: https://www.mindstick.com/forum/160732/chunk-arrays

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
