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)]
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
  }
 
上一篇:字符串需要掌握的内置方法


下一篇:阿里云服务器双十一配置仅需85元/年