Go-数组去重

来源:互联网 发布:网络诈骗防范措施 编辑:程序博客网 时间:2024/04/27 20:41
package main import(    "fmt"    "sort") func RemoveDuplicatesAndEmpty(a []string) (ret []string){    a_len := len(a)    for i:=0; i < a_len; i++{        if (i > 0 && a[i-1] == a[i]) || len(a[i])==0{            continue;        }        ret = append(ret, a[i])    }    return} func main(){    a := []string{"hello", "", "world", "yes", "hello", "nihao", "shijie", "hello", "yes", "nihao","good"}    sort.Strings(a)    fmt.Println(a)    fmt.Println(RemoveDuplicatesAndEmpty(a))}


0 0