Skip to content

Commit 423d35e

Browse files
Add solution for Challenge 27
1 parent eb9f7e6 commit 423d35e

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

challenge-27/submissions/hvijaycse/solution-template.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ func (s *Stack[T]) Push(value T) {
5050
// Pop removes and returns the top element from the stack
5151
// Returns an error if the stack is empty
5252
func (s *Stack[T]) Pop() (T, error) {
53-
// TODO: Implement this method
5453
var zero T
5554

5655
if s.IsEmpty() {
@@ -62,7 +61,6 @@ func (s *Stack[T]) Pop() (T, error) {
6261
return zero, nil
6362
}
6463

65-
6664
// Peek returns the top element without removing it
6765
// Returns an error if the stack is empty
6866
func (s *Stack[T]) Peek() (T, error) {
@@ -140,7 +138,6 @@ func (q *Queue[T]) Size() int {
140138

141139
// IsEmpty returns true if the queue contains no elements
142140
func (q *Queue[T]) IsEmpty() bool {
143-
// TODO: Implement this method
144141
return q.Size() == 0
145142
}
146143

@@ -156,27 +153,20 @@ type Set[T comparable] struct {
156153

157154
// NewSet creates a new empty set
158155
func NewSet[T comparable]() *Set[T] {
159-
// TODO: Implement this function
160156
return &Set[T]{
161157
ItemSet: map[T]struct{}{},
162158
}
163159
}
164160

165161
// Add adds an element to the set if it's not already present
166162
func (s *Set[T]) Add(value T) {
167-
// TODO: Implement this method
168163
s.mu.Lock()
169164
defer s.mu.Unlock()
170165
s.ItemSet[value] = struct{}{}
171166
}
172167

173168
// Remove removes an element from the set if it exists
174169
func (s *Set[T]) Remove(value T) {
175-
176-
if !s.Contains(value) {
177-
return
178-
}
179-
180170
s.mu.Lock()
181171
defer s.mu.Unlock()
182172
delete(s.ItemSet, value)
@@ -192,13 +182,16 @@ func (s *Set[T]) Contains(value T) bool {
192182

193183
// Size returns the number of elements in the set
194184
func (s *Set[T]) Size() int {
195-
// TODO: Implement this method
185+
s.mu.Lock()
186+
defer s.mu.Unlock()
196187
return len(s.ItemSet)
197188
}
198189

199190
// Elements returns a slice containing all elements in the set
200191
func (s *Set[T]) Elements() []T {
201-
// TODO: Implement this method
192+
193+
s.mu.Lock()
194+
defer s.mu.Unlock()
202195

203196
items := make([]T, len(s.ItemSet))
204197
i := 0
@@ -323,4 +316,3 @@ func RemoveDuplicates[T comparable](slice []T) []T {
323316
}
324317
return uniques
325318
}
326-

0 commit comments

Comments
 (0)