Cookies management by TermsFeed Cookie Consent

🗑️ Remove duplicates from any slice using Generics in Go

introduction slice generics generics-intro

Please consider supporting us by disabling your ad blocker

In one of our previous examples, we created a function that removes duplicate values from a slice in Go. This function, however, needs to be reimplemented each time the slice is of a different type. So, if we had []int and []string slices that we wanted to remove duplicates from, so far, we needed two functions: uniqueString() and uniqueInt(). But with the release of the Generics in Go 1.18, this is no longer necessary. We can write a single function that will work on any slice where the values satisfy the comparable constraint.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main

import "fmt"

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

type FruitRank struct {
    Fruit string
    Rank  uint64
}

func main() {
    fmt.Println(unique([]string{"abc", "cde", "efg", "efg", "abc", "cde"}))
    fmt.Println(unique([]int{1, 1, 2, 2, 3, 3, 4}))

    fruits := []FruitRank{
        {
            Fruit: "Strawberry",
            Rank:  1,
        },
        {
            Fruit: "Raspberry",
            Rank:  2,
        },
        {
            Fruit: "Blueberry",
            Rank:  3,
        },
        {
            Fruit: "Blueberry",
            Rank:  3,
        },
        {
            Fruit: "Strawberry",
            Rank:  1,
        },
    }
    fmt.Println(unique(fruits))
}

Output:

[abc cde efg]
[1 2 3 4]
[{Strawberry 1} {Raspberry 2} {Blueberry 3}]

If you are not already familiar with the basics of Generics in Go, check out our series of articles: Introduction to Go Generics by example.

The function differs from the non-generic version only in that it uses a type parameter T with the comparable constraint. This is a built-in constraint that describes any type on which the == and != operators can be used.

The unique() function that we created is a universal duplicate removal function that, as you can see in the output of the example, works on any comparable slice such as []string, []int, or a defined struct slice.


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!

🔉 Reduce function using Generics in Go

Learn how to define a function to accumulate slice values using Generics
introduction generics generics-intro

🦾 Create a slice 'map' function using Generics in Go

Learn how to apply a function to all slice elements using Generics
introduction generics generics-intro

💂 Filter a slice using Generics in Go

Learn how to remove elements from a slice that do not meet a certain condition using Generics
introduction generics generics-intro