I have to insert K numbers which may range from 0 to 10^9. Later i want to know whether i have inserted a particular number or not. Here K ranges from 0 to 10^5.
Both the insertion and deletion have to be in constant time. I have looked at the C++ STL unordered_map
. But unordered_map
requires two parameters. I just want to insert a number not a key-value pair
.
I could simply use an array of bools like
bool numberExists[1000000000];
But initialising this to false will take a lot of time. As is said i wanted constant time insertion and lookup.
What data structure should i use...?