处理字符串数组中的数据

I have an array of strings:

var a [5]string
a[0] = "red|apple|1"
a[1] = "yellow|apple|3"
a[2] = "red|apple|4"

I need to convert the data to array or map or whatever but if first and second values between pipes are the same the numbers should be added, so my desired output would be:

var b [5]string
a[0] = "red|apple|5"
a[1] = "yellow|apple|3"

Any help appreciated.

You can use a map to achieve that.

var a [3]string
a[0] = "red|apple|1"
a[1] = "yellow|apple|3"
a[2] = "red|apple|4"

b := make(map[string]int)

for _, s := range a {
    k := s[:len(s)-2]
    v := s[len(s)-1:]
    i, err:= strconv.Atoi(v) // please, check for errors
    if err != nil {
        fmt.Println("bad input")
        continue
    }
    b[k] += i
}

for k, v := range b {
    fmt.Println(k, v)
}

Output:

yellow|apple 3
red|apple 5