Home > Backend Development > Golang > How to Efficiently Remove Duplicate Values from Go Slices?

How to Efficiently Remove Duplicate Values from Go Slices?

Susan Sarandon
Release: 2024-12-18 10:25:10
Original
453 people have browsed it

How to Efficiently Remove Duplicate Values from Go Slices?

How to Efficiently Remove Duplicate Values from Slices in Go

Newcomers to Go often encounter the challenge of removing duplicate values from slices. While iterating through the slice and manually checking for duplicates may seem like a viable approach, it is inefficient. This article presents optimal solutions for removing duplicates generically and specifically for strings and integers.

Generic Solution

The following generic function utilizes a map to keep track of the unique elements and append them accordingly:

func removeDuplicate[T comparable](sliceList []T) []T {
    allKeys := make(map[T]bool)
    list := []T{}
    for _, item := range sliceList {
        if _, value := allKeys[item]; !value {
            allKeys[item] = true
            list = append(list, item)
        }
    }
    return list
}
Copy after login

String-Specific Solution

For improved efficiency, a string-specific solution can be used:

func removeDuplicateStr(strSlice []string) []string {
    allKeys := make(map[string]bool)
    list := []string{}
    for _, item := range strSlice {
        if _, value := allKeys[item]; !value {
            allKeys[item] = true
            list = append(list, item)
        }
    }
    return list
}
Copy after login

Integer-Specific Solution

Similarly, an integer-specific solution offers further optimization:

func removeDuplicateInt(intSlice []int) []int {
    allKeys := make(map[int]bool)
    list := []int{}
    for _, item := range intSlice {
        if _, value := allKeys[item]; !value {
            allKeys[item] = true
            list = append(list, item)
        }
    }
    return list
}
Copy after login

Usage

These functions can be utilized by passing the slice containing duplicate values and receiving a new slice with only the unique elements:

// Sample usage
cities := []string{"Mumbai", "Delhi", "Ahmedabad", "Mumbai", "Bangalore", "Delhi", "Kolkata", "Pune"}
uniqueCities := removeDuplicate(cities)
Copy after login

Conclusion

The presented solutions provide optimal methods for removing duplicate values from slices in Go, allowing for efficient code and optimal performance.

The above is the detailed content of How to Efficiently Remove Duplicate Values from Go Slices?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template