nightingale1/vendor/github.com/toolkits/pkg/slice/unique.go

100 lines
1.3 KiB
Go

package slice
import "strings"
func UniqueInt64(s []int64) []int64 {
size := len(s)
if size == 0 {
return []int64{}
}
m := make(map[int64]struct{})
for i := 0; i < size; i++ {
m[s[i]] = struct{}{}
}
realLen := len(m)
ret := make([]int64, realLen)
idx := 0
for key := range m {
ret[idx] = key
idx++
}
return ret
}
func UniqueInt(s []int) []int {
size := len(s)
if size == 0 {
return []int{}
}
m := make(map[int]struct{})
for i := 0; i < size; i++ {
m[s[i]] = struct{}{}
}
realLen := len(m)
ret := make([]int, realLen)
idx := 0
for key := range m {
ret[idx] = key
idx++
}
return ret
}
func UniqueString(s []string) []string {
size := len(s)
if size == 0 {
return []string{}
}
m := make(map[string]struct{})
for i := 0; i < size; i++ {
m[s[i]] = struct{}{}
}
realLen := len(m)
ret := make([]string, realLen)
idx := 0
for key := range m {
ret[idx] = key
idx++
}
return ret
}
func UniqueTrimString(s []string) []string {
size := len(s)
if size == 0 {
return []string{}
}
m := make(map[string]struct{})
for i := 0; i < size; i++ {
item := strings.TrimSpace(s[i])
if item == "" {
continue
}
m[item] = struct{}{}
}
realLen := len(m)
ret := make([]string, realLen)
idx := 0
for key := range m {
ret[idx] = key
idx++
}
return ret
}