Skip to content

Unordered Set

#include <iostream>
#include<list>
using namespace std;
template <typename T>

class HashSet {
    private:
        static const int size = 1000;
        list<T> table[size];
        int hsh(T key){
            return hash<T>{} (key) % size; // inbuild hash function
        }
    public:
        void insert(T key){
            int index = hsh(key);
            for( const T& val : table[index]){
                if(val == key) return;
            }
            table[index].push_back(key);
        }
        void remove(T key) {
		    int index = hash(key);
		    table[index].remove(key);
	    }
	    bool contains(T key) {
    		int index = hash(key);
    		for (const T& val : table[index]) {
    			if (val == key) {
    				return true;
    			}
    		}
    		return false;
	    }
};
int main() {
    // Write C++ code here
    HashSet<int> hs;    
    hs.insert(4);

    return 0;
}