Golang Program To Remove Duplicates From An Array


In this article, we will write a go language program to remove duplicates from an array. To achieve this we will be using two approaches. In the first one, we will use the array of strings, and in the second one; we will be using an array of integers.

Method 1: Remove Duplicates from an Array of Strings using The Make() Function

In this example, we will write a go language program to remove duplicates from the array of strings using a user-defined function. The function will accept the array of strings as an argument and will return the final array after removing the repeating values from it.

Syntax

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size, and capacity as arguments

Algorithm

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

Step 2 − Now, make a function named removeDuplicate(). This function accepts the array as an argument and returns the result containing the unique set of values.

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 strings and values as Boolean by default the values stored by the map are false.

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

Step 6 − Now, we need to start the main() function.

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

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

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

Example

Golang program to remove duplicates from an array of strings using an external function

package main
import "fmt"

// function to remove duplicate values
func removeDuplicates(s []string) []string {
   bucket := make(map[string]bool)
   var result []string
   for _, str := range s {
      if _, ok := bucket[str]; !ok {
         bucket[str] = true
         result = append(result, str)
      }
   }
   return result
}
func main() {
   
   // creating an array of strings
   array := []string{"abc", "cde", "efg", "efg", "abc", "cde"}
   fmt.Println("The given array of string is:", array)
   fmt.Println()

   // calling the function
   result := removeDuplicates(array)
   fmt.Println("The array obtained after removing the duplicate entries is:", result)
}

Output

The given array of string is: [abc cde efg efg abc cde]
The array obtained after removing the duplicate entries is: [abc cde efg]

Method: Remove Duplicates from an Array of Integers using an Append Function

The following code illustrates how we can remove the duplicate values from an array of integers using auser-defined function

Syntax

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes a number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

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 return the new array thus 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 duplicates from an array of integers using an append 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]

Conclusion

We have successfully compiled and executed a go language program to remove duplicate values from the array. We have used two programs here in the first one we are removing the values from the array of strings and in the second one from the array of integers. Both examples use user-defined functions to achieve the result.

Updated on: 06-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements