github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xslice/unique.go (about) 1 package xslice 2 3 func UniqueSlice(list []interface{}) []interface{} { 4 res := make(map[interface{}]struct{}) 5 for _, x := range list { 6 res[x] = struct{}{} 7 } 8 result := make([]interface{}, 0) 9 for k := range res { 10 result = append(result, k) 11 } 12 return result 13 } 14 15 func UniqueStringSlice(list []string) []string { 16 res := make(map[string]struct{}) 17 for _, x := range list { 18 res[x] = struct{}{} 19 } 20 result := make([]string, 0) 21 for k := range res { 22 result = append(result, k) 23 } 24 return result 25 } 26 27 func UniqueIntSlice(list []int) []int { 28 res := make(map[int]struct{}) 29 for _, x := range list { 30 res[x] = struct{}{} 31 } 32 result := make([]int, 0) 33 for k := range res { 34 result = append(result, k) 35 } 36 return result 37 } 38 39 func UniqueInt64Slice(list []int64) []int64 { 40 res := make(map[int64]struct{}) 41 for _, x := range list { 42 res[x] = struct{}{} 43 } 44 result := make([]int64, 0) 45 for k := range res { 46 result = append(result, k) 47 } 48 return result 49 } 50 51 func UniqueInt32Slice(list []int32) []int32 { 52 res := make(map[int32]struct{}) 53 for _, x := range list { 54 res[x] = struct{}{} 55 } 56 result := make([]int32, 0) 57 for k := range res { 58 result = append(result, k) 59 } 60 return result 61 } 62 63 func UniqueInt16Slice(list []int16) []int16 { 64 res := make(map[int16]struct{}) 65 for _, x := range list { 66 res[x] = struct{}{} 67 } 68 result := make([]int16, 0) 69 for k := range res { 70 result = append(result, k) 71 } 72 return result 73 } 74 75 func UniqueInt8Slice(list []int8) []int8 { 76 res := make(map[int8]struct{}) 77 for _, x := range list { 78 res[x] = struct{}{} 79 } 80 result := make([]int8, 0) 81 for k := range res { 82 result = append(result, k) 83 } 84 return result 85 }