Because it only uses a few discrete variables (max and second_max) to track state, and never allocates new arrays or data structures.
Space Complexity Optimization for Extreme Values
Why is the one-pass method O(1) space?
The answer
More FAQs in Space Complexity Optimization for Extreme Values
It means the algorithm requires a fixed, constant amount of extra memory, regardless of how large the input data becomes.
No, converting an array to a Set to remove duplicates requires O(N) extra memory, which is inefficient compared to ignoring duplicates logically with an O(1) variable check.
In real-world systems, RAM is a finite and expensive resource. Algorithms that copy massive datasets unnecessarily can crash servers with OutOfMemory errors.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.