Golang Program to Remove Repeated Elements From an Array


In this tutorial, we will write a go language program to remove duplicate elements from an array. By removing the duplicate entries, we mean that we wish to remove a value repeating multiple times. In this tutorial, we are using examples of an array of integers as well as an array of strings.

Method 1: Remove Duplicate Values from an Array using an External Function

The following code illustrates how we can remove duplicate values from an array of integers using a user-defined function.

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − Now, make a function named removeDuplicate() that accepts an array as an argument and returns an array after removing all the duplicate entries.

Step 3 − This function uses a for loop to iterate over the array.

Step 4 − Here we have created a map that has keys as integers and values as Boolean by default the values stored by the map_var are false.

Step 5 − During each iteration from the array, we are checking the value of map_var if it is false then we have to take that value and append it into the new array created above.

Step 6 − Repeat this process until all the array values are checked and then return the new array just formed.

Step 7 − Now, we need to start the main function.

Step 8 − Initialize an array arr of integers, store values to it, and print the array on the screen.

Step 9 − Now call the removeDuplicate function by passing the array created above as an argument to it.

Step 10 − Store the result obtained in an array called result and print the array on the screen.

Example

Golang program to remove duplicate values from an array using an external function.

package main
import "fmt"

// making a function named removeDuplicate()
func removeDuplicate(arr [8]int) []int {
   map_var := map[int]bool{}
   result := []int{}
   for e := range arr {
      if map_var[arr[e]] != true {
         map_var[arr[e]] = true
         result = append(result, arr[e])
      }
   }
   return result
}
func main() {
   arr := [8]int{1, 2, 2, 4, 4, 5, 7, 5}
   fmt.Println("The unsorted array entered is:", arr)
   result := removeDuplicate(arr)
   fmt.Println("The array obtained after removing the duplicate values is:", result)
}

Output

The unsorted array entered is: [1 2 2 4 4 5 7 5]
The array obtained after removing the duplicate values is: [1 2 4 5 7]

Method 2: Remove Duplicate Elements from an Array without using the Maps

Let us now look at another example of how we can remove duplicate entries from an array without using the maps.

Algorithm

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

Step 2 − Call the main() function.

Step 3 − Initialize and store the elements in an array of integers and print it on the screen.

Step 4 − Iterate over the array using for loops to check if the current element is equal to the next element or not.

Step 5 − If both the elements are equal then remove the duplicate element using the for loop and decrement the size of the array by 1 by doing size--

Step 6 − Once iterated over the whole array print the elements of the new array obtained on the screen using fmt.Println() function.

Example

GoLang Program to Remove Duplicate Elements from an Array.

package main
import "fmt"
func main() {
   arr := []int{1, 2, 2, 4, 4, 5, 7, 5}
   fmt.Println("The unsorted array entered is:", arr)
   size := len(arr)
   for i := 0; i < size; i++ {
      for j := i + 1; j < size; j++ {
         if arr[i] == arr[j] {
            for k := j; k < size-1; k++ {
               arr[k] = arr[k+1]
            }
            size--
            j--
         }
      }
   }
   fmt.Println("The elements of array obtained after removing the duplicate values is:")
   for i := 0; i < size; i++ {
      fmt.Println(arr[i])
   }
}

Output

The unsorted array entered is: [1 2 2 4 4 5 7 5]
The elements of array obtained after removing the duplicate values is:
1
2
4
5
7

Conclusion

We have successfully compiled and executed a golang program to remove duplicate elements from an array along with examples.

Updated on: 06-Jan-2023

312 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements