- Learn Data Structures and Algorithms with Golang
- Bhagvan Kommadi
- 137字
- 2021-06-24 15:37:49
The Union method
The Union method on the Set class returns a unionSet that consists of a union of set and anotherSet. Both sets are traversed through integerMap keys, and union set is updated with elements from the sets, as follows:
//Union method returns the set which is union of the set with anotherSet
func (set *Set) Union(anotherSet *Set) *Set{
var unionSet = & Set{}
unionSet.New()
var value int
for(value,_ = range set.integerMap){
unionSet.AddElement(value)
}
for(value,_ = range anotherSet.integerMap){
unionSet.AddElement(value)
}
return unionSet
}
The example output after invoking the union method with the anotherSet parameter is as follows. A new unionSet is created. The current set and another set values are iterated. Every value is added to the union set:

Let's take a look at the main method in the next section.