slice是列表,无序排列,允许重复。
map是字典,有序排列,不允许重复。
去重最简单的方式就是用map的特性来进行。
func main()
{
list := []string {"hello", "hello", "world"}
list =Deduplication(list)
fmt.Println(list)
}
//用Go泛型。
func Deduplication[T comparable](list []T) []T {
temp := make(map[T]struct{})
index := 0
for _, v := range list { temp[v] = struct{}{} }
tempList := make([]T, len(temp))
for key := range temp {
tempList[index] = key index++
}
return tempList
}