Cookies management by TermsFeed Cookie Consent

✂️ Remove duplicates from a slice in Go

Last updated:
introduction slice

Please consider supporting us by disabling your ad blocker

If you want to remove duplicate values from a slice in Go, you need to create a function that:

The most important part of the unique() function is to create a set of seen values, which is a map of type map[string]bool, so it can check if the element is already in the resulting slice.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package main

import "fmt"

func unique(s []string) []string {
    inResult := make(map[string]bool)
    var result []string
    for _, str := range s {
        if _, ok := inResult[str]; !ok {
            inResult[str] = true
            result = append(result, str)
        }
    }
    return result
}

func main() {
    fmt.Println(unique([]string{"abc", "cde", "efg", "efg", "abc", "cde"}))
}

Output:

[abc cde efg]

As an output, you get a new slice with all values unique. The function works in the same way for slices of different types. You just need to replace all occurrences of type string with another selected type.

With the release of Generics in Go 1.18, you can write a universal function to remove duplicates without having to re-implement it every time a slice is of a different type. Check out how to do it here.


Thank you for being on our site 😊. If you like our tutorials and examples, please consider supporting us with a cup of coffee and we'll turn it into more great Go examples.

Have a great day!

🗑️ Remove duplicates from any slice using Generics in Go

Learn how to create a slice with unique values using Generics
introduction slice generics generics-intro

🧑‍🤝‍🧑 Copy a slice in Go

Learn how to make a deep copy of a slice
introduction slice

🔎 Check if the slice contains the given value in Go

Learn how to write a function that checks if a slice has a specific value
introduction slice