How to Remove duplicate values from Slice?
Example
package main
import (
"fmt"
)
func unique(intSlice []int) []int {
keys := make(map[int]bool)
list := []int{}
for _, entry := range intSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func main() {
intSlice := []int{1,5,3,6,9,9,4,2,3,1,5}
fmt.Println(intSlice)
uniqueSlice := unique(intSlice)
fmt.Println(uniqueSlice)
}
Output
[1 5 3 6 9 9 4 2 3 1 5]
[1 5 3 6 9 4 2]
Most Helpful This Week
Simple function with parameters in Golang
Sample program to create csv and write data
Replace first occurrence of string using Regexp
Add N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time
How to check if a map contains a key in Go?
What is GOPATH?