Skip to content

Commit 3a18733

Browse files
author
Shuo
authored
Merge pull request #526 from openset/develop
Add: Design HashSet
2 parents 2b62ebd + ad93bfb commit 3a18733

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
11
package design_hashset
2+
3+
type MyHashSet struct {
4+
data map[int]bool
5+
}
6+
7+
/** Initialize your data structure here. */
8+
func Constructor() MyHashSet {
9+
return MyHashSet{make(map[int]bool, 0)}
10+
}
11+
12+
func (this *MyHashSet) Add(key int) {
13+
this.data[key] = true
14+
}
15+
16+
func (this *MyHashSet) Remove(key int) {
17+
delete(this.data, key)
18+
}
19+
20+
/** Returns true if this set contains the specified element */
21+
func (this *MyHashSet) Contains(key int) bool {
22+
return this.data[key]
23+
}
24+
25+
/**
26+
* Your MyHashSet object will be instantiated and called as such:
27+
* obj := Constructor();
28+
* obj.Add(key);
29+
* obj.Remove(key);
30+
* param_3 := obj.Contains(key);
31+
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
11
package design_hashset
2+
3+
import "testing"
4+
5+
func TestDesignHashset(t *testing.T) {
6+
obj := Constructor()
7+
obj.Add(1)
8+
obj.Add(2)
9+
output := obj.Contains(1)
10+
output = !obj.Contains(3) && output
11+
obj.Add(2)
12+
output = obj.Contains(2) && output
13+
obj.Remove(2)
14+
output = !obj.Contains(2) && output
15+
if !output {
16+
t.Fatalf("output: %v, expected: %v", output, true)
17+
}
18+
}

0 commit comments

Comments
 (0)