Go Program to Merge Two Arrays

Introduction

Merging two arrays is a common operation where elements from both arrays are combined into a single array. This guide will demonstrate how to write a Go program that merges two arrays into one.

Problem Statement

Create a Go program that:

  • Prompts the user to enter the number of elements in two arrays.
  • Takes input for the elements of both arrays.
  • Merges the two arrays into a single array.
  • Displays the merged array.

Example:

  • Input: Array 1: [1, 3, 5], Array 2: [2, 4, 6]
  • Output: Merged array: [1, 3, 5, 2, 4, 6]

Solution Steps

  1. Import the fmt Package: Use import "fmt" to include the fmt package for formatted I/O operations.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Input the Number of Elements: Use fmt.Scanln to take input from the user for the number of elements in both arrays.
  4. Input the Array Elements: Use loops to input the elements of both arrays from the user.
  5. Merge the Arrays: Concatenate the two arrays into a single array.
  6. Display the Merged Array: Use fmt.Println to display the merged array.

Go Program

package main

import "fmt"

/**
 * Go Program to Merge Two Arrays
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare variables to hold the number of elements in the arrays
    var n1, n2 int

    // Step 2: Prompt the user to enter the number of elements in the first array
    fmt.Print("Enter the number of elements in the first array: ")
    fmt.Scanln(&n1)

    // Step 3: Declare the first array and input its elements
    array1 := make([]int, n1)
    fmt.Println("Enter the elements of the first array:")
    for i := 0; i < n1; i++ {
        fmt.Scanln(&array1[i])
    }

    // Step 4: Prompt the user to enter the number of elements in the second array
    fmt.Print("Enter the number of elements in the second array: ")
    fmt.Scanln(&n2)

    // Step 5: Declare the second array and input its elements
    array2 := make([]int, n2)
    fmt.Println("Enter the elements of the second array:")
    for i := 0; i < n2; i++ {
        fmt.Scanln(&array2[i])
    }

    // Step 6: Merge the two arrays
    mergedArray := append(array1, array2...)

    // Step 7: Display the merged array
    fmt.Println("Merged array:", mergedArray)
}

Explanation

Step 1: Declare Variables

  • The variables n1 and n2 are declared to store the number of elements in the first and second arrays, respectively.

Step 2: Input the Number of Elements for the First Array

  • The program prompts the user to enter the number of elements in the first array using fmt.Print. The fmt.Scanln function reads the input and stores it in the n1 variable.

Step 3: Declare and Input the Elements of the First Array

  • An array array1 is created using make to hold n1 integer elements. The program then uses a for loop to prompt the user to enter each element of the array, storing the input in the corresponding index of array1.

Step 4: Input the Number of Elements for the Second Array

  • The program repeats the process for the second array by prompting the user to enter the number of elements using fmt.Print and fmt.Scanln, storing the result in the n2 variable.

Step 5: Declare and Input the Elements of the Second Array

  • An array array2 is created using make to hold n2 integer elements. The program uses another for loop to prompt the user to enter each element of array2.

Step 6: Merge the Two Arrays

  • The append function is used to concatenate array2 to array1, creating the mergedArray.

Step 7: Display the Merged Array

  • The merged array is displayed using fmt.Println.

Output Example

Example 1:

Enter the number of elements in the first array: 3
Enter the elements of the first array:
1
3
5
Enter the number of elements in the second array: 3
Enter the elements of the second array:
2
4
6
Merged array: [1 3 5 2 4 6]

Example 2:

Enter the number of elements in the first array: 4
Enter the elements of the first array:
10
20
30
40
Enter the number of elements in the second array: 2
Enter the elements of the second array:
50
60
Merged array: [10 20 30 40 50 60]

Conclusion

This Go program demonstrates how to merge two arrays by concatenating them into a single array. It covers basic programming concepts such as arrays, loops, and array manipulation. This example is useful for beginners learning Go programming and understanding how to work with multiple arrays and combine their elements.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top