unique

package
v0.0.0-...-f32f910 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2022 License: MIT Imports: 0 Imported by: 0

README

Unique

Finds unique elements in a slice.

This is a helper package for common functionality in my Go code. See Creating Unique Slices in Go for a blog post about the methodology being used here.

Basic usage:

import "github.com/bbengfort/x/unique"

names := []string{"foo", "bar", "foo", "baz", "zap", "bar"}
deduped := unique.Strings(names)
// []string{"bar", "baz", "foo", "zap"}

There is a unique function for all the types that I've needed; sorry if there isn't one for the type you were hoping for.

Documentation

Overview

Package unique implements type-specific functions to deduplicate a slice and return a slice with only the unique elements in it. It uses an intermediate map to store values already seen in the slice, then returns the new slice. Note that no guarantees are made about ordering.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Float32s

func Float32s(input []float32) []float32

Float32s returns a slice of float32 without any duplicates in it.

Example
vals := []float32{1.2, 2.2, 3.3, 1.2, 2.2, 3.1, 1.2, 2.2, 3.1, 3.3, 1.2}
deduped := Float32s(vals)
fmt.Println(deduped)
Output:

[1.2 2.2 3.3 3.1]

func Float64s

func Float64s(input []float64) []float64

Float64s returns a slice of float64 without any duplicates in it.

Example
vals := []float64{1.2, 2.2, 3.3, 1.2, 2.2, 3.1, 1.2, 2.2, 3.1, 3.3, 1.2}
deduped := Float64s(vals)
fmt.Println(deduped)
Output:

[1.2 2.2 3.3 3.1]

func Int32s

func Int32s(input []int32) []int32

Int32s returns a slice of int32 without any duplicates in it.

Example
vals := []int32{-3, -2, -1, 1, 2, -3, 1, -2, 3, 1, 2, 3, 4, 5}
deduped := Int32s(vals)
fmt.Println(deduped)
Output:

[-3 -2 -1 1 2 3 4 5]

func Int64s

func Int64s(input []int64) []int64

Int64s returns a slice of int64 without any duplicates in it.

Example
vals := []int64{-3, -2, -1, 1, 2, -3, 1, -2, 3, 1, 2, 3, 4, 5}
deduped := Int64s(vals)
fmt.Println(deduped)
Output:

[-3 -2 -1 1 2 3 4 5]

func Ints

func Ints(input []int) []int

Ints returns a slice of ints without any duplicates in it.

Example
vals := []int{-3, -2, -1, 1, 2, -3, 1, -2, 3, 1, 2, 3, 4, 5}
deduped := Ints(vals)
fmt.Println(deduped)
Output:

[-3 -2 -1 1 2 3 4 5]

func Strings

func Strings(input []string) []string

Strings returns a slice of strings without any duplicates in it.

Example
vals := []string{"foo", "bar", "foo", "baz", "bar", "zap", "foo"}
deduped := Strings(vals)
fmt.Println(deduped)
Output:

[foo bar baz zap]

func UInt32s

func UInt32s(input []uint32) []uint32

UInt32s returns a slice of uint32 without any duplicates in it.

Example
vals := []uint32{1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 5}
deduped := UInt32s(vals)
fmt.Println(deduped)
Output:

[1 2 3 4 5]

func UInt64s

func UInt64s(input []uint64) []uint64

UInt64s returns a slice of uint64 without any duplicates in it.

Example
vals := []uint64{1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 5}
deduped := UInt64s(vals)
fmt.Println(deduped)
Output:

[1 2 3 4 5]

func UInts

func UInts(input []uint) []uint

UInts returns a slice of uints without any duplicates in it.

Example
vals := []uint{1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 5}
deduped := UInts(vals)
fmt.Println(deduped)
Output:

[1 2 3 4 5]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL