Golang Program to merge two integer arrays without using library function


An array in go language is defined as the data structure that is used to store elements in contiguous memory locations. Arrays allow us to search and store elements at constant time. arrays use index to store elements which starts from 0 and goes to n – 1, where n is the length of the array.

Here we will use two methods in the first method we will implement the logic in the main section of the program while in the second one we will use an external function.

Method 1:Using For Loops In The Main

Here we will use a for loop to iterate over the array and will copy each element of both the arrays in the new array and will print the final array on the screen.

Algorithm

  • Step 1 − Import the fmt package that allows us to print anything on the screen.

  • Step 2 − Now call the main() function.

  • Step 3 − Initialize two arrays of integer type and store values to them. Further print these two arrays on the screen.

  • Step 4 − Store the length of arr1 in a variable called j

  • Step 5 − initialize a third empty array whose length is the sum of length of first and second array of type integers.

  • Step 6 − We have then used a for loop to iterate over the first array and store all its values in arr3.

  • Step 7 − Also iterate over the second array and store its values in arr3 after the values of arr1.

  • Step 8 − To achieve this we have used a variable j and we are storing the values in arr3 through j’s index.

  • Step 9 − We have by now achieved a third array which contains the elements of first and second array

  • Step 10 − Printing this array on the screen using fmt.Println() function.

Example

In this example we will write a golang program to merge two arrays of integers using for loops in the main() section of the program.

package main
import "fmt"
func main() {
   arr1 := [4]int{1, 2, 3, 4}
   fmt.Println("The first array entered is:", arr1)
   arr2 := [4]int{5, 6, 7, 8}
   var j int = len(arr1)
   var arr3 [8]int
   fmt.Println("The second array entered is:", arr2)
   for i := 0; i < len(arr1); i++ {
      arr3[i] = arr1[i]
   }
   for i := 0; i < len(arr2); i++ {
      arr3[j] = arr2[i]
      j = j + 1
   }
   fmt.Println("The final array obtained after merging the first two arrays is:", arr3)
}

Output

The first array entered is: [1 2 3 4]
The second array entered is: [5 6 7 8]
The final array obtained after merging the first two arrays is: [1 2 3 4 5 6 7 8]

Method 2: Using An External User Defined Function

The function we create will accept the two arrays of integers as arguments and will return the final array after merging the two.

Algorithm

  • Step 1 − Import the fmt package that allows us to print anything on the screen.

  • Step 2 − Now, create a function called mergeArrays() to merge the two arrays. This function accepts the two arrays as argument and returns the final array after merging them.

  • Step 3 − Inside this function store the length of arr1 in a variable called j. further initialize a third empty array whose length is the sum of length of first and second array of type integers.

  • Step 4 − We have then used a for loop to iterate over the first array and store all its values in arr3.

  • Step 5 − Also iterate over the second array and store its values in arr3 after the values of arr1.

  • Step 6 − To achieve this we have used a variable j and we are storing the values in arr3 through j’s index.

  • Step 7 − We have by now achieved a third array which contains the elements of first and second array.

  • Step 8 − Now call the main() function. In the main() section of the program Initialize two arrays of integer type and store values to them. Further print these two arrays on the screen.

  • Step 9 − Now, call the mergeArrays() function by passing the two arrays as arguments to it and store the result obtained in a separate variable called result.

  • Step 10 − Now, print this array of result on the screen using fmt.Println() function.

Example

In this example, we will use an external user defined function to merge the two given arrays of integers into the third array.

package main
import "fmt"
func mergeArrays(arr1 [4]int, arr2 [4]int) [8]int {
   var arr3 [8]int
   var j int = len(arr1)
   for i := 0; i < len(arr1); i++ {
      arr3[i] = arr1[i]
   }
   for i := 0; i < len(arr2); i++ {
      arr3[j] = arr2[i]
      j = j + 1
   }
   return arr3
}
func main() {
   arr1 := [4]int{1, 2, 3, 4}
   fmt.Println("The first array entered is:", arr1)
   arr2 := [4]int{5, 6, 7, 8}
   fmt.Println("The second array entered is:", arr2)
   result := mergeArrays(arr1, arr2)
   fmt.Println("The final array obtained after merging the first two arrays is:", result)
}

Output

The first array entered is: [1 2 3 4]
The second array entered is: [5 6 7 8]
The final array obtained after merging the first two arrays is: [1 2 3 4 5 6 7 8]

Conclusion

We have successfully compiled and executed a go language program to merge two arrays along with examples. we have used two programs here in the first program we are using a for loop in the main() section of the program to merge two arrays in the third one while in the last program we are using an external user defined function to achieve the result.

Updated on: 16-Feb-2023

283 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements