Skip to content

Commit

Permalink
Added nil checks to sets union and intersect operations (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
noyshabtay authored Jan 31, 2024
1 parent 1a26427 commit 041a424
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions datastructures/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,34 @@ func (set *Set[T]) ToSlice() []T {
return slice
}

func (setA *Set[T]) Intersect(setB *Set[T]) *Set[T] {
intersectedSet := MakeSet[T]()
bigSet, smallSet := setB, setA
if setA.Size() > setB.Size() {
bigSet, smallSet = setA, setB
func (set *Set[T]) Intersect(setB *Set[T]) *Set[T] {
intersectSet := MakeSet[T]()
if setB == nil {
return intersectSet
}
bigSet, smallSet := setB, set
if set.Size() > setB.Size() {
bigSet, smallSet = set, setB
}

for key := range smallSet.container {
if bigSet.Exists(key) {
intersectedSet.Add(key)
intersectSet.Add(key)
}
}
return intersectedSet
return intersectSet
}

func (setA *Set[T]) Union(setB *Set[T]) *Set[T] {
unionedSet := MakeSet[T]()
for key := range setA.container {
unionedSet.Add(key)
func (set *Set[T]) Union(setB *Set[T]) *Set[T] {
if setB == nil {
return set
}
unionSet := MakeSet[T]()
for key := range set.container {
unionSet.Add(key)
}
for key := range setB.container {
unionedSet.Add(key)
unionSet.Add(key)
}
return unionedSet
return unionSet
}

0 comments on commit 041a424

Please sign in to comment.