Created
February 22, 2022 05:46
-
-
Save nidhi-canopas/14aab93167ef4d46fe40d34f46e9c0d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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