DEV Community

shubham200
shubham200

Posted on

set vs unordered_set..

when we compare the time complexity of set(O(log n) and unordered_set(O(1)),set seems to be slower as compared to unordered_set.
But in real life set is said to be more efficient to use as unordered_set (initially)uses hash function hash(n)=n which will lead to large no of collision and make the program inefficient.
on the other hand set uses self-balancing BST for the orderness of the elememts and its worst case complexity is O(log n).

note:- in case you can't afford (log n ) time complexity you can write your own efficient hash function.

Top comments (0)