Suppose I have a slice of strings like:
fruits := {"apple", "orange", "banana"}
and a map like
box:= map[string]int{
"chicken": 1,
"drinks": 4,
"apples": 42,
}
What is the most efficient way to check whether the box contains any apple, orange or banana?
Notice that here we seek not exact match but a key that CONTAINS certain strings. So simple key search does not work here.
I know I can extract keys from the map:
keys := make([]string)
for k := range box {
keys = append(keys, k)
}
And then iterate over both slices to search among the keys:
for _, f := range fruits {
for _, k in keys {
if strings.Contains(k, f) {
fmt.Println("Fruit found!")
}
}
But that refutes the advantage of using map instead of slice for string searchs. So is there better way to do so?