1 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊
這應(yīng)該以非常有效的方式解決您的問題:O(N) package main
import (
"fmt"
"strconv"
)
func main() {
testStringSlice := []string{"apple", "banana", "apple", "peer", "apple"}
outputStringSlice := numberItems(testStringSlice)
fmt.Println(outputStringSlice)
}
func numberItems (arr []string) []string{
m := make(map[string]int)
for idx, item := range arr {
if count, ok := m[item]; ok {
m[item] = count + 1
arr[idx] = item + strconv.Itoa(m[item])
} else {
m[item] = 1
}
}
return arr
}
- 1 回答
- 0 關(guān)注
- 78 瀏覽
添加回答
舉報(bào)