Skip to content

Archive

Deduplication

2 articles
Go 13 Sep 2026 4 min read

Remove Adjacent Duplicates with slices.Compact in Go

slices.Compact removes repeated values only when they occur next to each other. That detail makes it different from set-based deduplication: the function collapses equal runs, preserves their order, and modifies the supplied slice storage. The operation fits data that is already grouped by value, including sorted slices and streams that naturally produce repeated adjacent states. It does not search the full slice for every matching value. Compact collapses consecutive runs The generic signature accepts slices whose element type is comparable:

Go 11 Sep 2026 4 min read

Remove Adjacent Duplicates from Go Slices with slices.Compact

Duplicate values often arrive in runs: repeated status events, sorted IDs, or adjacent tokens produced by a parser. When only consecutive duplicates need to disappear, slices.Compact handles the operation without a handwritten loop. The distinction is specific. slices.Compact collapses adjacent equal values; it doesn’t search the entire slice for duplicates. It also modifies the slice’s backing storage, so callers need to account for aliasing. Remove adjacent duplicates with slices.Compact Pass a slice whose element type is comparable and assign the returned slice: