Skip to content

Commit 6698164

Browse files
authored
Merge pull request #1 from ghoti143/add-unique
Add unique
2 parents 233b92e + 0711d8c commit 6698164

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ func main() {
1818
newArray = sf.Map(newArray, func(item int) int { return item * 3 })
1919
newArray = sf.Filter(newArray, func(item int) bool { return item%2 == 0 })
2020
fmt.Println(newArray)
21+
22+
duplicates := []string{"cat", "dog", "bird", "cat"}
23+
deduped := sf.Unique(duplicates)
24+
fmt.Println(deduped)
2125
}
22-
```
26+
```

main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ func Filter[T any](s []T, f func(T) bool) []T {
1919
}
2020
return r[:counter]
2121
}
22+
23+
func Unique[T comparable](s []T) []T {
24+
inResult := make(map[T]bool)
25+
var result []T
26+
for _, str := range s {
27+
if _, ok := inResult[str]; !ok {
28+
inResult[str] = true
29+
result = append(result, str)
30+
}
31+
}
32+
return result
33+
}

0 commit comments

Comments
 (0)