I have a map of slices that I need to remove duplicates from. I think I'm close to the solution but I'm missing something that I can't quite figure out.
Expected output: map[key1:[1 2 3] key2:[1 2 3]]
Actual output: map[key2:[1 2 3]]
package main
import "fmt"
func main() {
x := make(map[string][]string)
keys := make(map[string]bool)
result := make(map[string][]string)
x["key1"] = append(x["key1"], "1")
x["key1"] = append(x["key1"], "1")
x["key1"] = append(x["key1"], "2")
x["key1"] = append(x["key1"], "3")
x["key1"] = append(x["key1"], "3")
x["key2"] = append(x["key2"], "1")
x["key2"] = append(x["key2"], "2")
x["key2"] = append(x["key2"], "2")
x["key2"] = append(x["key2"], "3")
fmt.Println(x)
for k, e := range x{
for _, v := range e {
if _, val := keys[v]; !val {
keys[v] = true
result[k] = append(result[k], v)
}
}
}
fmt.Println(result)
}
Example on the playground here: Go Playground