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
}
Removing duplicates from a slice in Go filters out repeated items while keeping the original order. It works like a bouncer checking a guest list: if a name hasn't been seen yet, they get in; if they've already entered, they are skipped. You use this when you need a clean list of unique entries from messy data.