How to Remove Duplicates from a Slice in Go

Remove duplicates from a Go slice by tracking seen items in a map and appending unique values to a new slice.

Use a map to track seen items and append only unique values to a new slice.

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