Skip to content

Commit

Permalink
Add union, intersection, difference, subset to ZSet
Browse files Browse the repository at this point in the history
Signed-off-by: Devansh Singh <[email protected]>
  • Loading branch information
Devansh3712 committed Feb 20, 2024
1 parent 7a75bc5 commit a5b5e95
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
1 change: 0 additions & 1 deletion set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func (s *Set) Remove(element string) error {

func (s1 *Set) Union(s2 Set) Set {
elements := NewSet()

for element := range s1.Elements {
elements.Add(element)
}
Expand Down
40 changes: 40 additions & 0 deletions zset/zset.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,43 @@ func (z *ZSet) Members() []string {

return z.Elements.members()
}

func (z1 *ZSet) Union(z2 ZSet) ZSet {
elements := NewZSet()
for _, element := range z1.Members() {
elements.Add(element)
}
for _, element := range z2.Members() {
elements.Add(element)
}
return elements
}

func (z1 *ZSet) Intersection(z2 ZSet) ZSet {
elements := NewZSet()
for _, element := range z1.Members() {
if z2.Exists(element) {
elements.Add(element)
}
}
return elements
}

func (z1 *ZSet) Difference(z2 ZSet) ZSet {
elements := NewZSet()
for _, element := range z1.Members() {
if !z2.Exists(element) {
elements.Add(element)
}
}
return elements
}

func (z1 *ZSet) Subset(z2 ZSet) bool {
for _, element := range z1.Members() {
if !z2.Exists(element) {
return false
}
}
return true
}

0 comments on commit a5b5e95

Please sign in to comment.