方法一:
//这种发放适用于string,int,float等切片,会对切片中的元素进行排序
package test
import (
"sort"
)
func SliceRemoveDuplicates(slice []string) []string {
sort.Strings(slice)
i := 0
var j int
for {
if i >= len(slice)-1 {
break
}
for j = i + 1; j < len(slice) && slice[i] == slice[j]; j++ {
}
slice = append(slice[:i+1], slice[j:]...)
i++
}
return slice
}
方法二:
package test
func RemoveDuplicate(list *[]int) []int {
var x []int
for _, i := range *list {
if len(x) == 0 {
x = append(x, i)
} else {
for k, v := range x {
if i == v {
break
}
if k == len(x)-1 {
x = append(x, i)
}
}
}
}
return x
}