Convert a 2D array into a 1D array using C#, Python, Java, and C programs with code examples.

Array definition: An array is a data structure that can store multiple values of the same type in a contiguous block of memory. 

Arrays can have one or more dimensions, depending on how many indices are needed to access an element. A one-dimensional (1D) array is often referred to as a vector, while a two-dimensional (2D) array is commonly known as a matrix.

In this article, we will discuss some of the common methods to convert a 2D array into a 1D array using C#, Python, Java, and C programs with code examples.

Convert 2d array to a 1d array in C#

Here is an example of using a nested loop to convert a 2D array into a 1D array in C# in row-major order:

using System;

class Program
{
    static void Main()
    {


      // Creating a 2d Array with 2 rows and three columns
       int[,] twoDArray = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

        int rows = twoDArray.GetLength(0); // Get the number of rows
        int cols = twoDArray.GetLength(1); // Get the number of columns

       
       // Creating a 1D array with enough space
        int[] oneDArray = new int[rows * cols];
        int index = 0;

        for (int i = 0; i < rows; i++) // Loop over the rows of the 2D array
        {
            for (int j = 0; j < cols; j++) // Loop over the columns of the 2D
            {
                // Copy the element from the 2D array to the 1D array
                oneDArray[index++] = twoDArray[i, j];
            }
        }

        // Display the 1D array
        Console.WriteLine(string.Join(", ", oneDArray));
    }
}

The resulting array will look like this:

1, 2, 3, 4, 5, 6

Code Explanation: 

In this C# example, we define the dimensions of the 2D array. Subsequently, we create a 1D array with the correct size. During this process, each element is transferred to the corresponding position in the newly created 1D array from the 2D array.

Ultimately, we iterate through the elements of the 1D array to display the result.

Convert 2d array to 1d array in python

In Python, we can also use a list comprehension to iterate over the 2D array and append its elements into a 1D array. We can use the for keyword to loop over the rows and columns of the 2D array, and use the in keyword to access the elements.

For example, if we want to convert the 2D array in row-major order, we can use this list comprehension:

two_d_array = [[1, 2, 3], [4, 5, 6]]

one_d_array = [element for row in two_d_array for element in row]

# Display the 1D array
print(one_d_array)

Result:

[1, 2, 3, 4, 5, 6]

Convert 2d array to a 1d array in Java

This Java example follows a similar approach to C# to convert a 2D array into a 1D array. We are iterating through the 2D array and populating the 1D array accordingly.

import java.util.Arrays;

public class ArrayConversion {
    public static void main(String[] args) {
        int[][] twoDArray = { { 1, 2, 3 }, { 4, 5, 6 } };

        int rows = twoDArray.length;
        int cols = twoDArray[0].length;

        int[] oneDArray = new int[rows * cols];
        int index = 0;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                oneDArray[index++] = twoDArray[i][j];
            }
        }

        // Display the 1D array
        System.out.println(Arrays.toString(oneDArray));
    }
}

Output:

[1, 2, 3, 4, 5, 6]

Convert 2d array to a 1d array in C

In C, we manually calculate the number of rows and columns, then iterate through the 2D array to fill the 1D array.

#include <stdio.h>

int main() {
    int twoDArray[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };

    int rows = sizeof(twoDArray) / sizeof(twoDArray[0]);
    int cols = sizeof(twoDArray[0]) / sizeof(twoDArray[0][0]);

    int oneDArray[rows * cols];
    int index = 0;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            oneDArray[index++] = twoDArray[i][j];
        }
    }

    // Display the 1D array
    for (int i = 0; i < rows * cols; i++) {
        printf("%d ", oneDArray[i]);
    }

    return 0;
}

Output:

1 2 3 4 5 6 

Recommended Articles:

Shekh Ali
5 2 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments