RosettaCodeData/Task/Remove-duplicate-elements/Go/remove-duplicate-elements-2.go

24 lines
402 B
Go
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
package main
import "fmt"
func uniq(list []int) []int {
2015-02-20 00:35:01 -05:00
unique_set := make(map[int]int, len(list))
i := 0
for _, x := range list {
if _, there := unique_set[x]; !there {
unique_set[x] = i
i++
}
}
result := make([]int, len(unique_set))
for x, i := range unique_set {
result[i] = x
}
return result
2013-04-10 23:57:08 -07:00
}
func main() {
2015-02-20 00:35:01 -05:00
fmt.Println(uniq([]int{1, 2, 3, 2, 3, 4})) // prints: [1 2 3 4]
2013-04-10 23:57:08 -07:00
}