0
0
GoProgramBeginner · 2 min read

Go Program to Remove Duplicates from Slice

In Go, you can remove duplicates from a slice by using a map to track seen elements and then appending only unique elements to a new slice, like this: seen := make(map[int]bool); for _, v := range slice { if !seen[v] { seen[v] = true; result = append(result, v) } }.
📋

Examples

Input[1, 2, 2, 3, 4, 4, 5]
Output[1 2 3 4 5]
Input[10, 10, 10]
Output[10]
Input[]
Output[]
🧠

How to Think About It

To remove duplicates from a slice, think of checking each item and remembering if you have seen it before. Use a map to keep track of items you already added. If an item is new, add it to the result slice; if not, skip it.
📐

Algorithm

1
Create an empty map to record seen elements.
2
Create an empty slice to store unique elements.
3
Loop through each element in the original slice.
4
Check if the element is in the map; if not, add it to the map and append it to the unique slice.
5
Return the unique slice after the loop ends.
💻

Code

go
package main

import "fmt"

func removeDuplicates(slice []int) []int {
    seen := make(map[int]bool)
    var result []int
    for _, v := range slice {
        if !seen[v] {
            seen[v] = true
            result = append(result, v)
        }
    }
    return result
}

func main() {
    nums := []int{1, 2, 2, 3, 4, 4, 5}
    unique := removeDuplicates(nums)
    fmt.Println(unique)
}
Output
[1 2 3 4 5]
🔍

Dry Run

Let's trace the slice [1, 2, 2, 3, 4, 4, 5] through the code

1

Initialize map and result slice

seen = {}, result = []

2

Process element 1

1 not in seen, add 1: seen = {1:true}, result = [1]

3

Process element 2

2 not in seen, add 2: seen = {1:true, 2:true}, result = [1, 2]

4

Process element 2 again

2 in seen, skip

5

Process element 3

3 not in seen, add 3: seen = {1:true, 2:true, 3:true}, result = [1, 2, 3]

6

Process element 4

4 not in seen, add 4: seen = {1:true, 2:true, 3:true, 4:true}, result = [1, 2, 3, 4]

7

Process element 4 again

4 in seen, skip

8

Process element 5

5 not in seen, add 5: seen = {1:true, 2:true, 3:true, 4:true, 5:true}, result = [1, 2, 3, 4, 5]

IterationElementSeen Map KeysResult Slice
11[1][1]
22[1 2][1 2]
32[1 2][1 2]
43[1 2 3][1 2 3]
54[1 2 3 4][1 2 3 4]
64[1 2 3 4][1 2 3 4]
75[1 2 3 4 5][1 2 3 4 5]
💡

Why This Works

Step 1: Use a map to track seen elements

The map stores each element as a key with a boolean value to quickly check if it appeared before.

Step 2: Append only new elements

When an element is not in the map, it is added to the result slice to keep only unique values.

Step 3: Return the unique slice

After processing all elements, the result slice contains no duplicates and is returned.

🔄

Alternative Approaches

Sort and remove duplicates in place
go
package main

import (
    "fmt"
    "sort"
)

func removeDuplicatesSorted(slice []int) []int {
    if len(slice) == 0 {
        return slice
    }
    sort.Ints(slice)
    j := 0
    for i := 1; i < len(slice); i++ {
        if slice[j] != slice[i] {
            j++
            slice[j] = slice[i]
        }
    }
    return slice[:j+1]
}

func main() {
    nums := []int{4, 2, 2, 3, 1, 4, 5}
    unique := removeDuplicatesSorted(nums)
    fmt.Println(unique)
}
This method sorts the slice first, then removes duplicates in place, saving space but changing order.
Use a map with struct{} for less memory
go
package main

import "fmt"

func removeDuplicates(slice []int) []int {
    seen := make(map[int]struct{})
    var result []int
    for _, v := range slice {
        if _, ok := seen[v]; !ok {
            seen[v] = struct{}{}
            result = append(result, v)
        }
    }
    return result
}

func main() {
    nums := []int{1, 2, 2, 3, 4, 4, 5}
    unique := removeDuplicates(nums)
    fmt.Println(unique)
}
Using struct{}{} instead of bool saves a tiny bit of memory because struct{}{} uses zero bytes.

Complexity: O(n) time, O(n) space

Time Complexity

The program loops through the slice once, checking and inserting elements into a map, which is O(1) on average, so total time is O(n).

Space Complexity

The map and result slice grow with the number of unique elements, so space is O(n) in the worst case.

Which Approach is Fastest?

Using a map is fastest for unsorted slices. Sorting first can reduce space but adds O(n log n) time.

ApproachTimeSpaceBest For
Map to track seenO(n)O(n)Unsorted slices, preserves order
Sort and remove in placeO(n log n)O(1)When order can change and space is limited
Map with struct{}{}O(n)O(n)Memory-efficient map usage
💡
Use a map to track seen elements for fast duplicate removal in Go slices.
⚠️
Beginners often forget to check if an element is already in the map before appending, causing duplicates to remain.