Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Go
seen := make(map[T]bool)
j := 0
for _, v := range x {
	if !seen[v] {
		x[j] = v
		j++
		seen[v] = true
	}
}
for i := j; i < len(x); i++ {
	x[i] = nil
}
x = x[:j]

Order is preserved.
Use this if T is a pointer type or reference type.
Discarded slots are set to nil, to avoid a memory leak.

This is O(n).
y := make(map[T]struct{}, len(x))
for _, v := range x {
	y[v] = struct{}{}
}
x2 := make([]T, 0, len(y))
for _, v := range x {
	if _, ok := y[v]; ok {
		x2 = append(x2, v)
		delete(y, v)
	}
}
x = x2

Original order is preserved.
T is the type of the items.
Iterate twice, from list to map, then from map to list.

This is O(n).
seen := make(map[T]bool)
j := 0
for _, v := range x {
	if !seen[v] {
		x[j] = v
		j++
		seen[v] = true
	}
}
x = x[:j]

The order is preserved.
Use this if T is not a pointer type or reference type.

This is O(n).
func deduplicate[S ~[]T, T comparable](x S) S {
	seen := make(map[T]bool)
	j := 0
	for _, v := range x {
		if !seen[v] {
			x[j] = v
			j++
			seen[v] = true
		}
	}
	var zero T
	for i := j; i < len(x); i++ {
		// Avoid memory leak
		x[i] = zero
	}
	return x[:j]
}

deduplicate is generic. Its type parameter T has a constraint: must be comparable with ==.
The order is preserved.
import (
	"fmt"
	"slices"
)
slices.Sort(x)
x = slices.Compact(x)

Does not maintain order. Sorts the list. Type has to be comparable.
(distinct x)

New implementation...
< >
programming-idioms.org