go 循环切片得到重复移除成别名

import "fmt"
func removeDuplicateElement(addrs []string) []string {
result :=
make([]string, 0, len(addrs))
temp :=
map[string]struct{}{}
idx :=
0
for _, item := range addrs {
if _, ok := temp[item]; !ok {
temp[item] =
struct{}{}
result =
append(result, item)
}
else {
idx++
item +=
fmt.Sprintf("(%v)", idx)
result =
append(result, item)
}
}
return result
}


func main() {
fmt.Println(removeDuplicateElement([]string{"fsdf","fdsfsdf","fsdf"}))



}



[fsdf fdsfsdf fsdf(1)]

 

posted @ 2022-02-25 17:21  技术颜良  阅读(29)  评论(0编辑  收藏  举报