Is there a way to write a generic array/slice deduplication in go, for []int
we can have something like (from http://rosettacode.org/wiki/Remove_duplicate_elements#Go ):
func uniq(list []int) []int {
unique_set := make(map[int] bool, len(list))
for _, x := range list {
unique_set[x] = true
}
result := make([]int, len(unique_set))
i := 0
for x := range unique_set {
result[i] = x
i++
}
return result
}
But is there a way to extend it to support any array? with a signature like:
func deduplicate(a []interface{}) []interface{}
I know that you can write that function with that signature, but then you can't actually use it on []int
, you need to create a []interface{}
put everything from the []int
into it, pass it to the function then get it back and put it into a []interface{}
and go through this new array and put everything in a new []int
.
My question is, is there a better way to do this?