C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Map: We create a map in removeDuplicates. This records ints as we encounter them in the slice.
For: We loop over the original slice in a for-loop. We record each int in the map. We then add all unencountered ints to the result slice.
Result: The slice returned by removeDuplicates has all duplicates removed, but everything else about the original slice is left the same.
Golang program that removes duplicate elements
package main
import "fmt"
func removeDuplicates(elements []int) []int {
// Use map to record duplicates as we find them.
encountered := map[int]bool{}
result := []int{}
for v := range elements {
if encountered[elements[v]] == true {
// Do not add duplicate.
} else {
// Record this element as an encountered element.
encountered[elements[v]] = true
// Append to result slice.
result = append(result, elements[v])
}
}
// Return the new slice.
return result
}
func main() {
elements := []int{100, 200, 300, 100, 200, 400, 0}
fmt.Println(elements)
// Test our method.
result := removeDuplicates(elements)
fmt.Println(result)
}
Output
[100 200 300 100 200 400 0]
[100 200 300 400 0]
First: We add all elements from the string slice to a string map. The value (bool) is not important here.
Finally: We loop over the map and add all keys to a resulting slice. The map may store its keys in any order. This affects the result.
Golang program that removes duplicates, ignores order
package main
import "fmt"
func removeDuplicatesUnordered(elements []string) []string {
encountered := map[string]bool{}
// Create a map of all unique elements.
for v:= range elements {
encountered[elements[v]] = true
}
// Place all keys from the map into a slice.
result := []string{}
for key, _ := range encountered {
result = append(result, key)
}
return result
}
func main() {
elements := []string{"cat", "dog", "cat", "bird"}
fmt.Println(elements)
// Remove string duplicates, ignoring order.
result := removeDuplicatesUnordered(elements)
fmt.Println(result)
}
Output
[cat dog cat bird]
[cat dog bird]
Warning: A nested loop has a major algorithmic inefficiency. If a slice becomes long, this Go program will not perform well.
Golang program that removes duplicates with nested loops
package main
import "fmt"
func removeDuplicates(elements []int) []int {
result := []int{}
for i := 0; i < len(elements); i++ {
// Scan slice for a previous element of the same value.
exists := false
for v := 0; v < i; v++ {
if elements[v] == elements[i] {
exists = true
break
}
}
// If no previous element exists, append this one.
if !exists {
result = append(result, elements[i])
}
}
return result
}
func main() {
elements := []int{10, 20, 30, 10, 10, 20, 40}
fmt.Println(elements)
// Test the nested loop method.
result := removeDuplicates(elements)
fmt.Println(result)
}
Output
[10 20 30 10 10 20 40]
[10 20 30 40]
And: If no duplicates are found, we avoid creating a new slice. This will optimize programs that rarely need the "dedupe" operation.