Time complexity of Hash table

It’s impossible to know in advance how many collisions you will get with your hash function, as well as things like needing to resize. This can add an element of unpredictability to the performance of a hash table, making it not true O(1). However, virtually all hash table implementations offer O(1) on the vast, vast, vast majority of inserts. This is the same as array inserting – it’s O(1) unless you need to resize, in which case it’s O(n), plus the collision uncertainty.

In reality, hash collisions are very rare and the only condition in which you’d need to worry about these details is when your specific code has a very tight time window in which it must run. For virtually every use case, hash tables are O(1). More impressive than O(1) insertion is O(1) lookup.

Leave a Comment