Skip to content
Node: _.uniq(arr)github.com/sahilkhaire/gox/slice

Uniq

Overview

Uniq returns unique elements in first-seen order (lodash uniq).

If you are coming from Node.js, the closest pattern is _.uniq(arr).

Signature

go
func Uniq[T comparable](in []T) []T

Compare: Node.js · Standard Go · gox

js
const unique = _.uniq(items);
go
seen := make(map[T]struct{})
var unique []T
for _, v := range items { /* dedupe */ }
go
import "github.com/sahilkhaire/gox/slice"

unique := slice.Uniq(items)

Example

go
import "github.com/sahilkhaire/gox/slice"

unique := slice.Uniq(items)

Tips

Chain Filter, Map, and Reduce for lodash-style pipelines. Results are new slices — inputs are never mutated.

Standard library alternative

Use the standard library directly:

go
seen := make(map[T]struct{})
var unique []T
for _, v := range items { /* dedupe */ }

Back to slice package overview

MIT Licensed · Built for Node.js developers moving to Go