How to Remove Duplicate Values from Slice in Golang?

An array is a data structure. Similarly, in Golang we have slice which is more flexible, powerful, lightweight and convenient than array. As slice is more flexible than array therefore, its flexibility is determined in terms of its size. Just like an array, it has indexing value and length but its size is not fixed. When we declare a slice we do not specify the size of it.


[]mySlice{input1, input2, .........input n}

Moreover, a slice and an array are connected with each other, in a slice, there is a referencing to an underlying array. And in a slice, we can store duplicate elements.

Example: Here, we will see how to remove the duplicate elements from slice. We have defined a function where we are passing the slice values and using the map function we are checking the duplicates and removing them.





// Golang program to remove duplicate
// values from Slice
package main
import (
func removeDuplicateValues(intSlice []int) []int {
    keys := make(map[int]bool)
    list := []int{}
    // If the key(values of the slice) is not equal
    // to the already present value in new slice (list)
    // then we append it. else we jump on another element.
    for _, entry := range intSlice {
        if _, value := keys[entry]; !value {
            keys[entry] = true
            list = append(list, entry)
    return list
func main() {
    // Assigning values to the slice
    intSliceValues := []int{1,2,3,4,5,2,3,5,7,9,6,7}
    // Printing orignial value of slice
    // Calling function where we 
    // are removing the duplicates
    removeDuplicateValuesSlice := removeDuplicateValues(intSliceValues)
    // Printing the filtered slice
    // without duplicates



[1 2 3 4 5 2 3 5 7 9 6 7]
[1 2 3 4 5 7 9 6]


  • From the main function, we have declared a slice. Also we have print the original value of the slice.
  • We have defined a function where we are passing the slice original values and checking the duplicates.
  • Logic for duplicate check : For this we have defined another slice and assigning the first values by checking if the value already exists in the new slice or not. It returns the slice without duplicates.
  • We are calling removeDuplicateValues function from main function where the return slice from the function is printed.

My Personal Notes arrow_drop_up

Young professional who is passionate and dedicated about what she do and does not hesitate to go an extra mile to explore herself more as a professional Prerna holds merit in addition with two technical International

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.