In Go, sets are typically implemented using maps. You can check if two sets are equal by comparing their elements.
In this example,
set1
and set2
.set1 := map[int]bool{1: true, 2: true, 3: true}
set2 := map[int]bool{1: true, 2: true, 3: true}
func setsAreEqual(set1, set2 map[int]bool) bool {
if len(set1) != len(set2) {
return false
}
for key := range set1 {
if !set2[key] {
return false
}
}
return true
}
fmt.Println(setsAreEqual(set1, set2)) // Output: true
true
In this example,
set1
and set2
.set1 := map[int]bool{1: true, 2: true, 3: true}
set2 := map[int]bool{4: true, 5: true, 6: true}
func setsAreEqual(set1, set2 map[int]bool) bool {
if len(set1) != len(set2) {
return false
}
for key := range set1 {
if !set2[key] {
return false
}
}
return true
}
fmt.Println(setsAreEqual(set1, set2)) // Output: false
false
In this tutorial, we learned How to check if Two Sets are Equal in Go language with well detailed examples.