Go Program to Remove Duplicates from an Array

Introduction

Removing duplicates from an array is a common task in programming where we want to ensure that each element in the array appears only once. This guide will demonstrate how to write a Go program that removes duplicate elements from an array.

Problem Statement

Create a Go program that:

  • Prompts the user to enter the number of elements in an array.
  • Takes input for the elements of the array.
  • Removes any duplicate elements from the array.
  • Displays the array after removing duplicates.

Example:

  • Input: Array elements: [1, 2, 2, 3, 4, 4, 5]
  • Output: Array after removing duplicates: [1, 2, 3, 4, 5]

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 the array.
  4. Input the Array Elements: Use a loop to input the elements of the array from the user.
  5. Remove Duplicates: Use a map to track unique elements and filter out duplicates.
  6. Display the Array After Removing Duplicates: Use fmt.Println to display the array after removing duplicates.

Go Program

package main

import "fmt"

/**
 * Go Program to Remove Duplicates from an Array
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare a variable to hold the number of elements in the array
    var n int

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

    // Step 3: Declare an array to hold the elements
    array := make([]int, n)

    // Step 4: Input the elements of the array
    fmt.Println("Enter the elements of the array:")
    for i := 0; i < n; i++ {
        fmt.Scanln(&array[i])
    }

    // Step 5: Remove duplicates using a map to track unique elements
    uniqueElements := make(map[int]bool)
    result := []int{}

    for _, value := range array {
        if _, exists := uniqueElements[value]; !exists {
            uniqueElements[value] = true
            result = append(result, value)
        }
    }

    // Step 6: Display the array after removing duplicates
    fmt.Println("Array after removing duplicates:", result)
}

Explanation

Step 1: Declare Variables

  • The variable n is declared to store the number of elements in the array.

Step 2: Input the Number of Elements

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

Step 3: Declare the Array

  • An array array is created using make to hold n integer elements.

Step 4: Input the Array Elements

  • The program uses a for loop to prompt the user to enter each element of the array, storing the input in the corresponding index of array.

Step 5: Remove Duplicates

  • The program uses a map uniqueElements to track elements that have already been added to the result array result. As the program iterates over the original array, it checks if the element is already in the map. If it is not, the element is added to both the map and the result array. This effectively removes duplicates.

Step 6: Display the Array After Removing Duplicates

  • The program prints the result array, which contains the elements of the original array with duplicates removed.

Output Example

Example 1:

Enter the number of elements in the array: 7
Enter the elements of the array:
1
2
2
3
4
4
5
Array after removing duplicates: [1 2 3 4 5]

Example 2:

Enter the number of elements in the array: 5
Enter the elements of the array:
10
10
20
30
30
Array after removing duplicates: [10 20 30]

Conclusion

This Go program demonstrates how to remove duplicates from an array by using a map to track unique elements. It covers basic programming concepts such as arrays, loops, and map data structures. This example is useful for beginners learning Go programming and understanding how to filter and manipulate arrays effectively.

Leave a Comment

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

Scroll to Top