Skip to content

Instantly share code, notes, and snippets.

@nidhi-canopas
Last active February 27, 2022 20:12
Show Gist options
  • Save nidhi-canopas/83a47145c9c3ba2504cf87d74c2cdb3c to your computer and use it in GitHub Desktop.
Save nidhi-canopas/83a47145c9c3ba2504cf87d74c2cdb3c to your computer and use it in GitHub Desktop.
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