What makes sets faster than lists?

list: Imagine you are looking for your socks in your closet, but you don’t know in which drawer your socks are, so you have to search drawer by drawer until you find them (or maybe you never do). That’s what we call O(n), because in the worst scenario, you will look in all your drawers (where n is the number of drawers).

set: Now, imagine you’re still looking for your socks in your closet, but now you know in which drawer your socks are, say in the 3rd drawer. So, you will just search in the 3rd drawer, instead of searching in all drawers. That’s what we call O(1), because in the worst scenario you will look in just one drawer.

Leave a Comment