I have a simple gota series and I want to remove duplicate from it. how it is possible in go?
func main() {
new_series := series.New([]string{"b", "a", "c", "a", "d", "b"}, series.String, "COL.1")
fmt.Println(new_series)
}
[b a c a d b]
expected: [b a c d]
To represent a unique set of elements in go
, use a map
.
So to create a unique set of strings from a given list, use something like:
func setFromList(list []string) (set []string) {
ks := make(map[string]bool) // map to keep track of repeats
for _, e := range list {
if _, v := ks[e]; !v {
ks[e] = true
set = append(set, e)
}
}
return
}
and to apply this to an existing gota.Series
:
func uniqueGotaSeries(s series.Series) series.Series {
return series.New(setFromList(s.Records()), s.Type(), s.Name)
}
Working playground example: https://play.golang.org/p/zqQM-0XxLF5
Output:
Orig: [b a c a d b]
Unique: [b a c d]