diff --git a/HashSet.java b/HashSet.java new file mode 100644 index 00000000..1d2e2fab --- /dev/null +++ b/HashSet.java @@ -0,0 +1,49 @@ +// Time Complexity : O(n) for add, remove, and contains — each linearly scans the ArrayList +// Space Complexity : O(n) — stores up to n unique keys in the ArrayList +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : None + +class MyHashSet { + ArrayList hashSet = new ArrayList<>(); + + public MyHashSet() { + + } + + public void add(int key) { + boolean repeated = false; + for(int i=0;i < hashSet.size(); i++){ + if(hashSet.get(i)==key){ + repeated = true; + } + } + if(repeated== false){ + hashSet.add(key); + } + } + + public void remove(int key) { + for(int i=0;i