Java Solution for Design HashSet Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind the solution is to use a list of lists (buckets) to simulate a has
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind the solution is to use a list of lists (buckets) to simulate a hash set. Each bucket represents a hash value, and the elements are stored within the corresponding bucket based on their hash values.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. The MyHashSet class uses a list of lists (buckets) to store the values. The number of buckets is set to 1000 for simplicity.\n1. The add method calculates the hash value for the given key by taking the modulo of the key with the number of buckets. It then retrieves the corresponding bucket and checks if the key is already present in the bucket. If not, it adds the key to the bucket.\n1. The remove method calculates the hash value for the key, retrieves the corresponding bucket, and removes the key from the bucket if it exists.\n1. The contains method calculates the hash value for the key, retrieves the corresponding bucket, and checks if the key exists in the bucket.\n1. The modulo operator (%) is used to ensure that the hash value falls within the range of the number of buckets.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of adding an element, removing an element, and checking for containment is primarily determined by the number of elements in each bucket. In the worst case scenario, if all elements collide and end up in the same bucket, the time complexity for these operations would be O(n), where n is the number of elements in the bucket. However, assuming a relatively uniform distribution of elements across the buckets, the average time complexity would be O(1).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity of the solution is O(NUM_BUCKETS) since we are using a list of lists to store the buckets. In this case, it is O(1000), which can be simplified to O(1) as it is a constant value.\n\n# Code\n```\nclass MyHashSet {\n    private static final int NUM_BUCKETS = 1000;\n    private List<List<Integer>> buckets;\n    public MyHashSet() {\n        buckets = new ArrayList<>(NUM_BUCKETS);\n        for (int i = 0; i < NUM_BUCKETS; i++) \n        {\n            buckets.add(new ArrayList<>());\n        }\n    }\n    \n    public void add(int key) {\n        int hash = key % NUM_BUCKETS;\n        List<Integer> bucket = buckets.get(hash);\n        if (!bucket.contains(key)) \n        {\n            bucket.add(key);\n        }\n    }\n    \n    public void remove(int key) {\n        int hash = key % NUM_BUCKETS;\n        List<Integer> bucket = buckets.get(hash);\n        bucket.remove(Integer.valueOf(key));\n    }\n    \n    public boolean contains(int key) {\n        int hash = key % NUM_BUCKETS;\n        List<Integer> bucket = buckets.get(hash);\n        return bucket.contains(key);\n    }\n}\n\n/**\n * Your MyHashSet object will be instantiated and called as such:\n * MyHashSet obj = new MyHashSet();\n * obj.add(key);\n * obj.remove(key);\n * boolean param_3 = obj.contains(key);\n */\n```
['Java']