123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package buildin
- // InSlice 判断自字符串是否在另一个字符串内
- func InSlice[T comparable](item T, slice []T) bool {
- for _, s := range slice {
- if s == item {
- return true
- }
- }
- return false
- }
- // Intersection 求交集
- func Intersection[T comparable](a, b []T) []T {
- var res []T
- if len(a) == 0 || len(b) == 0 {
- return res
- }
- m := make(map[T]bool)
- for _, v := range a {
- m[v] = true
- }
- for _, v := range b {
- if m[v] {
- res = append(res, v)
- }
- }
- return res
- }
- // RemoveDuplicates 去除重复值
- func RemoveDuplicates[T comparable](slice []T) []T {
- unique := make(map[T]bool)
- result := make([]T, 0, len(slice))
- for _, s := range slice {
- if !unique[s] {
- unique[s] = true
- result = append(result, s)
- }
- }
- return result
- }
|