How to Efficiently Remove Duplicate Values from Slices in Go
Newcomers to Go often encounter the challenge of removing duplicate values from slices. While iterating through the slice and manually checking for duplicates may seem like a viable approach, it is inefficient. This article presents optimal solutions for removing duplicates generically and specifically for strings and integers.
Generic Solution
The following generic function utilizes a map to keep track of the unique elements and append them accordingly:
func removeDuplicate[T comparable](sliceList []T) []T { allKeys := make(map[T]bool) list := []T{} for _, item := range sliceList { if _, value := allKeys[item]; !value { allKeys[item] = true list = append(list, item) } } return list }
String-Specific Solution
For improved efficiency, a string-specific solution can be used:
func removeDuplicateStr(strSlice []string) []string { allKeys := make(map[string]bool) list := []string{} for _, item := range strSlice { if _, value := allKeys[item]; !value { allKeys[item] = true list = append(list, item) } } return list }
Integer-Specific Solution
Similarly, an integer-specific solution offers further optimization:
func removeDuplicateInt(intSlice []int) []int { allKeys := make(map[int]bool) list := []int{} for _, item := range intSlice { if _, value := allKeys[item]; !value { allKeys[item] = true list = append(list, item) } } return list }
Usage
These functions can be utilized by passing the slice containing duplicate values and receiving a new slice with only the unique elements:
// Sample usage cities := []string{"Mumbai", "Delhi", "Ahmedabad", "Mumbai", "Bangalore", "Delhi", "Kolkata", "Pune"} uniqueCities := removeDuplicate(cities)
Conclusion
The presented solutions provide optimal methods for removing duplicate values from slices in Go, allowing for efficient code and optimal performance.
The above is the detailed content of How to Efficiently Remove Duplicate Values from Go Slices?. For more information, please follow other related articles on the PHP Chinese website!