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 can I convert a string variable into Boolean, Integer or Float type in Golang?
Split URL and Get Parameters from URL
Regular expression for matching HH:MM time format in Golang
Creating a Function in Golang
How do you write multi-line strings in Go?
How to get first and last element of slice in Golang?
Various examples of printing and formatting in Golang
How to kill execution of goroutine?
Create and Print Multi Dimensional Slice in Golang
How to concatenate two or more slices in Golang?