Skip to content

Instantly share code, notes, and snippets.

@nidhi-canopas
Created February 22, 2022 05:46
Show Gist options
  • Select an option

  • Save nidhi-canopas/14aab93167ef4d46fe40d34f46e9c0d0 to your computer and use it in GitHub Desktop.

Select an option

Save nidhi-canopas/14aab93167ef4d46fe40d34f46e9c0d0 to your computer and use it in GitHub Desktop.
import "fmt"
func main() {
// define array of strings
fruits := []string{"Mango", "Grapes", "Kiwi", "Apple", "Grapes"}
fmt.Println("Array before removing duplicates : ", fruits)
// Array after duplicates removal
dulicatesRemovedArray := RemoveDuplicatesFromSlice(fruits)
fmt.Println("Array after removing duplicates : ", dulicatesRemovedArray)
}
func RemoveDuplicatesFromSlice(intSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range intSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
output:
Array before removing duplicates : [Mango Grapes Kiwi Apple Grapes]
Array after removing duplicates : [Mango Grapes Kiwi Apple]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment