Home
Go
slices.Compact (Dedup)
Updated Aug 31, 2025
Dot Net Perls

Compact, slices

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.

Example

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.

Part 1 This slice has the number 2 appearing twice, and the duplicate (or repeated) value is eliminated from the slice returned by slices.Compact.
Part 2 It is important when calling Compact() that elements be stored in groups of equal values—duplicates must be adjacent.
Part 3 The value 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]

Not 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.

Important A map could be used to implement a more powerful 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.

Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Aug 31, 2025 (new).
Home
Changes
© 2007-2025 Sam Allen