To check if a Set contains all the items of another Set in Go, you can use map data structure.
In this example,
set1
and set2
.set2
and check if it exists in set1
.set2
is not found in set1
, then set1
does not contain all items of set2
.package main
import (
"fmt"
)
func main() {
// Define Sets with items
set1 := map[string]bool{"apple": true, "banana": true, "orange": true, "grape": true, "melon": true}
set2 := map[string]bool{"banana": true, "orange": true}
// Check if set1 contains all items of set2
containsAllItems := true
for key := range set2 {
if _, exists := set1[key]; !exists {
containsAllItems = false
break
}
}
// Print result
fmt.Printf("Does set1 contain all items of set2? %t\n", containsAllItems)
}
Does set1 contain all items of set2? true
In this example,
set1
and set2
.set2
and check if it exists in set1
.set2
is not found in set1
, then set1
does not contain all items of set2
.package main
import (
"fmt"
)
func main() {
// Define Sets with items
set1 := map[string]bool{"apple": true, "banana": true, "orange": true, "grape": true, "melon": true}
set2 := map[string]bool{"banana": true, "peach": true}
// Check if set1 contains all items of set2
containsAllItems := true
for key := range set2 {
if _, exists := set1[key]; !exists {
containsAllItems = false
break
}
}
// Print result
fmt.Printf("Does set1 contain all items of set2? %t\n", containsAllItems)
}
Does set1 contain all items of set2? false
In this tutorial, we learned How to check if a Set contains all the items of Another Set in Go language with well detailed examples.