March 24
O(n)
What O(n) means and why a single pass usually grows with the input size.
What it means
O(n) means the amount of work grows along with the size of the input.
If the input doubles, the work usually doubles too.
Common shape
The classic case is a single pass:
for (const item of items) {
process(item)
}
You look at each element once.
When it matters
O(n) is often a good sign in array, string, and list problems.
It is not always the best possible answer.
But it is usually much better than repeating scans for no reason.
Better question
Instead of only asking “is this O(n)?”, ask:
- how many passes am I making?
- am I repeating work for no reason?
- would extra memory reduce the cost?
Share this page
Copy the link manually from the field below.