Go Program to Find duplicates in an Array

 

 

Duplicate Elements: Find duplicates in an array

This program demonstrates how to find duplicate elements in an array using Go. The algorithm iterates through the array and uses a map to track seen elements. If an element is found in the map, it is identified as a duplicate.

Go Program

package main

import "fmt"

// findDuplicates finds and returns duplicate elements in the given array.
func findDuplicates(arr []int) []int {
    seen := make(map[int]bool)
    duplicates := make(map[int]bool)

    for _, element := range arr {
        if seen[element] {
            duplicates[element] = true
        } else {
            seen[element] = true
        }
    }

    var result []int
    for key := range duplicates {
        result = append(result, key)
    }

    return result
}

func main() {
    array := []int{1, 2, 3, 4, 5, 6, 7, 2, 3, 8, 9, 10, 10}
    duplicates := findDuplicates(array)

    if len(duplicates) == 0 {
        fmt.Println("No duplicates found.")
    } else {
        fmt.Println("Duplicate elements:", duplicates)
    }
}

Explanation

The program includes the following components:

  • package main: Declares the main package.
  • import "fmt": Imports the fmt package for formatted I/O operations.
  • func findDuplicates(arr []int) []int: Defines a function that takes a slice of integers as input and returns a slice of integers containing the duplicate elements.
  • seen := make(map[int]bool): Initializes a map to keep track of elements that have been encountered in the array.
  • duplicates := make(map[int]bool): Initializes a map to store duplicate elements.
  • for _, element := range arr: Iterates through each element in the array.
  • if seen[element]: Checks if the element is already in the seen map. If it is, the element is a duplicate.
  • duplicates[element] = true: Adds the duplicate element to the duplicates map.
  • var result []int: Initializes a slice to store the duplicate elements.
  • for key := range duplicates: Iterates through the keys of the duplicates map and appends them to the result slice.
  • func main(): The main function that initializes an array and calls the findDuplicates function.
  • fmt.Println("Duplicate elements:", duplicates): Prints the duplicate elements.

The example array array := []int{1, 2, 3, 4, 5, 6, 7, 2, 3, 8, 9, 10, 10} contains the duplicates 2, 3, and 10. When the findDuplicates function is called, it will print:

No duplicates found.
Duplicate elements: [2, 3, 10]

 

Explanation

  • Go Program: The provided Go program utilizes maps to identify duplicate elements in an array. The findDuplicates function iterates through the array, using a map called seen to track elements that have been encountered. If an element is already in the seen map, it is added to a map called duplicates.
  • Output: The output of the program will list all the duplicate elements found in the array.

Leave a Reply

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