Last active
February 27, 2022 20:12
-
-
Save nidhi-canopas/83a47145c9c3ba2504cf87d74c2cdb3c to your computer and use it in GitHub Desktop.
This file contains 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() { | |
// create an array of strings | |
slice := []string{"apple", "grapes", "mango"} | |
// You can check by changing element to "orange" | |
if Contains(slice, "mango") { | |
fmt.Println("Slice contains element") | |
} else { | |
fmt.Println("Slice doesn't contain element") | |
} | |
} | |
func Contains(slice []string, element string) bool { | |
for _, i := range slice { | |
if i == element { | |
return true | |
} | |
} | |
return false | |
} | |
output: | |
Slice contains element |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment