Skip to content

Commit

Permalink
Add uniq
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Mar 1, 2022
1 parent 07130f9 commit cddd1fc
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions uniq/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"math/rand"
"time"
)

func uniq[T comparable](a []T) []T {
u := make([]T, 0, len(a))
m := make(map[T]bool)
for _, v := range a {
if _, ok := m[v]; !ok {
m[v] = true
u = append(u, v)
}
}
return u
}

func main() {
rand.Seed(time.Now().UnixNano())

fmt.Println(uniq([]int{4, 3, 2, 3, 1, 5, 1}))
}

0 comments on commit cddd1fc

Please sign in to comment.