The InterSect method

In the following code, the InterSect method on the Set class returns an intersectionSet that consists of the intersection of set and anotherSet. The set class is traversed through integerMap and checked against another Set to see if any elements exist:

//Intersect method returns the set which intersects with anotherSet

func (set *Set) Intersect(anotherSet *Set) *Set{
var intersectSet = & Set{}
intersectSet.New()
var value int
for(value,_ = range set.integerMap){
if anotherSet.ContainsElement(value) {
intersectSet.AddElement(value)
}
}
return intersectSet
}

The example output after invoking the intersect with the parameter of another Set is as follows. A new intersectSet is created. The current set is iterated and every value is checked to see if it is in another set. If the value is in another set, it is added to the set intersect:

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