Learn Data Structures and Algorithms with Golang
上QQ阅读APP看书,第一时间看更新

The main method – intersection and union

In the following code snippet, the main method calls intersect and union on the set class, passing the anotherSet parameter. The intersection and union sets are printed as follows:

// main method
func main() {
var set *Set
set = &Set{}
set.New()
set.AddElement(1)
set.AddElement(2)
fmt.Println("initial set", set)
fmt.Println(set.ContainsElement(1))
var anotherSet *Set
anotherSet = &Set{}
anotherSet.New()
anotherSet.AddElement(2)
anotherSet.AddElement(4)
anotherSet.AddElement(5) fmt.Println(set.Intersect(anotherSet))
fmt.Println(set.Union(anotherSet))
}

The main method takes two sets and finds the intersection and union of the sets.

Run the following command to execute the set.go file:

go run set.go

After executing the preceding command, we get the following output:

The next section talks about tuples, which are finite ordered sequences of objects.