Golang Program to get the flattened 1D array


In this tutorial, we will write a go language program to get the flattened 1D array from a multidimensional 2D array. A multi-dimensional array is an array of arrays, where each inner array represents a row of elements. A 1D array, on the other hand, is a single array that contains all the elements of the multi-dimensional array in a contiguous manner.

Syntax

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

Algorithm:

STEP 1 − First, we need to import fmt package.

STEP 2 −Then, we need to start the main() function. Inside this function initialize an array of integers and assign value to them. further print this array on the screen using fmt.Println() function.

STEP 3 − Create a function called flatArray() and inside this function create an empty array. use for loop to iterate over each element in the array and use append() function to append values to it.

STEP 4 − Now, return the final array thus formed. Inside the main() function call the flattenArray() function by passing the array as argument to the function.

STEP 5 − Store the final array returned by the function in a variable and print it on the screen. the final array obtained by the function is a 1-D array that is obtained by flattening the 2-D multidimensional array.

Example

In the following code, we are going to execute Golang program to get a flatted 1D array from a 2d array

package main
import "fmt"
func main() {

   // Initialize the multi-dimensional array
   arr := [][]int{
      {1, 2, 3, 4},
      {5, 6, 7, 8}, 
      {9, 10, 11, 12},
   }
   fmt.Println("The 2-D array is:", arr)

   // Flatten the array using the append function
   flatArr := flattenArray(arr)

   // Print the flattened array
   fmt.Println("The flattened array obtained from above multidimensional array is:", flatArr)
}
func flattenArray(arr [][]int) []int {

   // Initialize the flattened array
   flatArr := []int{}

   // Iterate through the rows of the array
   for _, row := range arr {

      // Append the current row to the flattened array
      flatArr = append(flatArr, row...)
   }
   return flatArr
}

Output

The 2-D array is: [[1 2 3 4] [5 6 7 8] [9 10 11 12]]
The flattened array obtained from above multidimensional array is: [1 2 3 4 5 6 7 8 9 10 11 12]

Conclusion

We have successfully compiled and executed a go language program to get the flattened array 1D array from the multidimensional 2D array. The above program uses an external function to achieve the result. The function accepts a 2D array and returns the final 1D array. inside this function we are using the go’s library function called append().

Updated on: 10-Feb-2023

839 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements