From e5fc4aab3295cc26f1d3096fe34520f2d7fee1f5 Mon Sep 17 00:00:00 2001 From: Siva512 <42845569+Siva512@users.noreply.github.com> Date: Sun, 6 Sep 2026 01:04:09 -0400 Subject: [PATCH] Update Hashset.java --- Sample.java | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/Sample.java b/Sample.java index 1739a9cb..1fe9056f 100644 --- a/Sample.java +++ b/Sample.java @@ -1,7 +1,48 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : +// Time Complexity : O(1) +// Space Complexity : O(N) +// Did this code successfully run on LeetCode: yes +// Any problem you faced while coding this : no // Your code here along with comments explaining your approach + + +class MyHashSet { + + private static final int SIZE = 420; + private LinkedList[] buckets; + + public MyHashSet() { + buckets = new LinkedList[SIZE]; + for( int i = 0; i(); + } + } + + private int hash(int key) { + return key % SIZE; + } + + public void add(int key) { + LinkedList bucket = buckets[hash(key)]; + if(!bucket.contains(key)) bucket.add(key); + } + + public void remove(int key) { + buckets[hash(key)].remove((Integer)key); + + + } + + public boolean contains(int key) { + return buckets[hash(key)].contains(key); + } +} + +/** + * Your MyHashSet object will be instantiated and called as such: + * MyHashSet obj = new MyHashSet(); + * obj.add(key); + * obj.remove(key); + * boolean param_3 = obj.contains(key); + */