1、现实代码
func RemoveDuplicate(list []string) []string {
sort.Strings(list)
i := 0
var newlist = []string{""}
for j := 0; j < len(list); j++ {
if strings.Compare(newlist[i], list[j]) == -1 {
newlist = append(newlist, list[j])
i++
}
}
return newlist
}
2、Test
func TestRemoveDuplicate(t *testing.T) {
var list = []string{"a", "q", "b", "a", "c", "f", "c", "d", "e", "s"}
list = RemoveDuplicate(list)
t.Logf("%s", list)
}
3、结果
=== RUN TestRemoveDuplicate
TestRemoveDuplicate: strings_test.go:8: [ a b c d e f q s]
--- PASS: TestRemoveDuplicate (0.00s)