Sling Academy
Home/Golang/How to remove duplicates from a slice in Go

How to remove duplicates from a slice in Go

Last updated: November 21, 2024

In this article, we'll explore how to remove duplicate elements from a slice in Go. A slice is a dynamic, flexible view into the elements of an array, and addressing duplicates can be a common task. We will start with basic implementations and then move to more advanced and efficient techniques.

Basic Method: Using a Map

One of the straightforward ways to remove duplicates is using a map to keep track of the elements you've already seen.

package main

import (
    "fmt"
)

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

func main() {
    sampleSlice := []int{1, 2, 2, 3, 4, 4, 5}
    fmt.Println("Original Slice:", sampleSlice)
    fmt.Println("Slice without duplicates:", removeDuplicates(sampleSlice))
}

The above code snippet defines a function that iterates through the slice, using a map to store each unique element, ensuring duplicates are filtered out in the process.

Intermediate Method: Removing Duplicates with Custom Function

With deeper understanding, you can build reusable functions or methods encapsulating duplicate removal.

package main

import (
    "fmt"
)

func removeDuplicatesString(slice []string) []string {
    dedupMap := make(map[string]bool)
    result := []string{}
    for _, value := range slice {
        if !dedupMap[value] {
            dedupMap[value] = true
            result = append(result, value)
        }
    }
    return result
}

func main() {
    sampleSlice := []string{"apple", "banana", "apple", "grape", "banana"}
    fmt.Println("Original Slice:", sampleSlice)
    fmt.Println("Slice without duplicates:", removeDuplicatesString(sampleSlice))
}

This function operates similarly to the integer example but handles a slice of strings, promoting code reuse across various data types.

Advanced Method: Using Set Data Structure

While Go doesn’t have a built-in set type, we can use a map to mimic set behavior effectively. Here we handle a more complex data manipulation by applying the functionality to structures or atypical scenarios.

package main

import (
    "fmt"
)

type person struct {
    name string
    age  int
}

func removeDuplicatePersons(slice []person) []person {
    dedupMap := make(map[person]bool)
    result := []person{}
    for _, value := range slice {
        if !dedupMap[value] {
            dedupMap[value] = true
            result = append(result, value)
        }
    }
    return result
}

func main() {
    sampleSlice := []person{
        {name: "Alice", age: 30},
        {name: "Bob", age: 25},
        {name: "Alice", age: 30},
        {name: "Tom", age: 40},
    }
    fmt.Println("Original Slice:", sampleSlice)
    fmt.Println("Slice without duplicates:", removeDuplicatePersons(sampleSlice))
}

In the advanced example above, an identical approach is taken to handle deduplication of slices composed of a struct, demonstrating the flexibility of map-based duplicate removal even for composite data types.

By understanding and implementing these techniques, you can efficiently handle duplicate elements in slices across various use cases in Go.

Next Article: Sorting a slice of integers in Go

Previous Article: Ways to iterate through a slice in Go

Series: Working with Slices in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant