Java Solution
Idea\nloadFactor tells when to perform re-hashing. I set it to 75% meaning if we have filled 75% of the container, we need to perform rehash. The reason for doi
**Idea**\n`loadFactor` tells when to perform re-hashing. I set it to 75% meaning if we have filled 75% of the container, we need to perform rehash. The reason for doing this is to "shorten" the individual chains at each bucket. That way, we reduce the time for `contains` and `remove` in the average case.\n\n**My thoughts**\nThere is an optimization to maintain these chains as BST for faster search. But there is an overhead of maintaing the balaced BST and extra space for node structure per entry. Rather, we can always re-hash to keep the lengths of the chains reasonable and there by limiting the worst case search for an entry. Of course, the hash function plays a crucial role in keeping the chains shorter by distributing the keys uniformly across buckets. The initial capcity and loadFactor are other knobs that we can tune depending on the actual workload we are targetting.\n\n**Update**: Thanks to [fhmeansrock](https://leetcode.com/fhmeansrock). I obviously missed the following check -  `add(key)` must check `contains(key)` to avoid taking in the duplicates. Ideally, `add(key)` must return a boolean value to indicate the presence of the key.\n\n```\nclass MyHashSet {\n    List<Integer>[] container = null;\n    int cap = 1000;\n    double loadFactor = 0.75;\n    int count = 0; \n    /** Initialize your data structure here. */\n    public MyHashSet() {\n        container = new LinkedList[cap];\n    }\n    \n    public void add(int key) {\n        if(contains(key)) return;\n        if(loadFactor*cap == count){\n            count = 0;\n            //rehash\n            cap *= 2;\n            List<Integer>[] oldC = container;\n            container = new LinkedList[cap];\n            for(int i=0; i < oldC.length; i++){\n                List<Integer> list = oldC[i];\n                if(list != null){\n                    for(int entry : list)\n                       this.add(entry); \n                }\n            }\n        }\n        int hash = key % cap;\n        if(container[hash] == null)\n            container[hash] = new LinkedList<>();\n        container[hash].add(key);\n        ++count;\n    }\n    \n    public void remove(int key) {\n        int hash = key % cap;\n        List<Integer> list = container[hash];\n        if(list != null){\n            Iterator<Integer> itr = list.iterator();\n            while(itr.hasNext())\n                if(itr.next() == key){\n                    itr.remove();\n                    --count;\n\t\t\t\t\tbreak;\n                }\n        }\n    }\n    \n    /** Returns true if this set contains the specified element */\n    public boolean contains(int key) {\n        int hash = key % cap;\n        List<Integer> list = container[hash];\n        if(list != null){\n            Iterator<Integer> itr = list.iterator();\n            while(itr.hasNext())\n                if(itr.next() == key)\n                    return true;\n        }\n        return false;\n    }\n}\n```
[]