There are many ways to remove duplicate elements from a slice in Go. If we have a sorted slice, we can use slices.Compact()
. Compact()
will only remove repeated elements.
The slice does not need to be sorted, but equal elements need to be stored adjacent to one another. For a method that removes duplicates in a completely unsorted slice, a map could be used.
This example uses the slices.Compact
func
three times, and it shows the func
works as expected on sorted slices where equal elements are grouped together.
slices.Compact
.Compact()
that elements be stored in groups of equal values—duplicates must be adjacent.nil
, in a slice of pointers, is considered a single value, so repeated nil
values will be eliminated.package main
import (
"fmt"
"slices"
)
func main() {
// Part 1: an int slice that has a repeat element.
items := []int{1, 2, 2, 3}
items = slices.Compact(items)
fmt.Println(items)
// Part 2: with Compact, the slice does not need to be sorted, but repeat items must be adjacent.
items2 := []int{10, 100, 1, 1, 1, -50, -50}
items2 = slices.Compact(items2)
fmt.Println(items2)
// Part 3: nil items will be compacted too.
cat := "cat"
items3 := []*string{&cat, nil, nil, &cat}
items3 = slices.Compact(items3)
fmt.Println(items3)
}[1 2 3]
[10 100 1 -50]
[0xc000022070 <nil> 0xc000022070]
dedup
Suppose we call Compact()
on a slice of 0, 1 and 0—the second zero will not be eliminated because Compact()
only handles repeat or adjacent duplicate values.
dedup()
method, but this would likely be slower in the case of a sorted slice.With slices.Compact
we have a convenient way to dedup
(remove duplicates from) a slice of any type of elements. Compact()
is a generic method in Go, which makes it easily reusable.