diff --git a/problems/design-hashset/design_hashset.go b/problems/design-hashset/design_hashset.go index 6cf8e4a0a..78b071bb7 100644 --- a/problems/design-hashset/design_hashset.go +++ b/problems/design-hashset/design_hashset.go @@ -1 +1,31 @@ package design_hashset + +type MyHashSet struct { + data map[int]bool +} + +/** Initialize your data structure here. */ +func Constructor() MyHashSet { + return MyHashSet{make(map[int]bool, 0)} +} + +func (this *MyHashSet) Add(key int) { + this.data[key] = true +} + +func (this *MyHashSet) Remove(key int) { + delete(this.data, key) +} + +/** Returns true if this set contains the specified element */ +func (this *MyHashSet) Contains(key int) bool { + return this.data[key] +} + +/** + * Your MyHashSet object will be instantiated and called as such: + * obj := Constructor(); + * obj.Add(key); + * obj.Remove(key); + * param_3 := obj.Contains(key); + */ diff --git a/problems/design-hashset/design_hashset_test.go b/problems/design-hashset/design_hashset_test.go index 6cf8e4a0a..cb85ea4f1 100644 --- a/problems/design-hashset/design_hashset_test.go +++ b/problems/design-hashset/design_hashset_test.go @@ -1 +1,18 @@ package design_hashset + +import "testing" + +func TestDesignHashset(t *testing.T) { + obj := Constructor() + obj.Add(1) + obj.Add(2) + output := obj.Contains(1) + output = !obj.Contains(3) && output + obj.Add(2) + output = obj.Contains(2) && output + obj.Remove(2) + output = !obj.Contains(2) && output + if !output { + t.Fatalf("output: %v, expected: %v", output, true) + } +}