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
How to remove symbols from a string in Golang?
Regular expression to extract numbers from a string in Golang
How to use for and foreach loop?
Pass different types of arguments in variadic function
Passing multiple string arguments to a variadic function
How to get first and last element of slice in Golang?