I am a casual gopher and I would need some help/tips on how to improve the following task.
I have two slices:
- the first slice
s
contains strings, those are not unique, as from time to time the same string is repeated twice or multiple times. - the second slice
n
contains integers, its content refers to the iteration in which each string was generated from a loop above.
As a minimal example:
s := []string{"a", "b", "c", "c", "e", "c", "e", "d", "e"} //the strings
n := []int{1, 1, 1, 2, 2, 3, 3, 4, 4} // the iteration that produced the strings
Essentially, the string βaβ was generated during the iteration 1, same for βbβ, while βcβ was generated in iterations 1, 2, and 3β¦
I need now to split the slice n
into sub-slices defined by the βgroupedβ slice s
:
//my end results would be
r := [][]int{{1}, {1}, {1, 2, 3}, {4}, {2, 3, 4}}
Essentially, the firs sub-slice is based on the string βaβ, which is generated only in iteration 1, the second sub-slice is βbβ also generated only in 1, then there is βcβ generated at 1,2, and 3β¦
I thought to make use of a map since keys are unique.
Again:
//my map end result
m := make(map[string][]int)
m["a"] = []int{1}
m["b"] = []int{1}
m["c"] = []int{1, 2, 3}
m["d"] = []int{4}
m["e"] = []int{2, 3, 4}
The following code works just fine, but it is super slow when the input slices (s
and n
) are in the range of several thousand elements.
package main
import (
"fmt"
)
func main() {
s := []string{"a", "b", "c", "c", "e", "c", "e", "d", "e"}
n := []int{1, 1, 1, 2, 2, 3, 3, 4, 4}
computedm := make(map[string][]int)
for i := 0; i < len(s); i++ {
x := indexof(s, s[i])
var y []int
for _, p := range x {
y = append(y, n[p])
}
computedm[s[i]] = y
}
fmt.Println(computedm)
}
func indexof(ss []string, s string) []int {
var res []int
for i, j := range ss {
if j == s {
res = append(res, i)
}
}
return res
}
How can I make it faster? lightning fastβ¦