Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions problems/design-hashset/design_hashset.go
Original file line number Diff line number Diff line change
@@ -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);
*/
17 changes: 17 additions & 0 deletions problems/design-hashset/design_hashset_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}