What is the fastest way to find Nth biggest number of an INT array?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The fastest way to find the nth biggest number depends on what "fastest" means for your input size and usage pattern. If you only need one answer from one array, the best average-time algorithm is usually Quickselect. If you want simpler code or the data stream is ongoing, a heap can be the better tradeoff.
A full sort works, but it does more work than necessary. Sorting the entire array is O(n log n), while selection algorithms can often find the nth largest element in linear average time.
Use Quickselect For One-Off Selection
Quickselect is related to Quicksort, but instead of sorting both partitions, it only recurses into the side that contains the target element. That gives an average complexity of O(n) with in-place partitioning.
The code converts the "nth largest" request into a zero-based index for the element that would appear in sorted order. That is why the target is len(nums) - n.
Use A Heap When n Is Small Or Data Is Streaming
If n is much smaller than the array size, a min-heap of size n is often attractive. It gives O(n log k) time, where k is n, and only stores the top candidates seen so far.
This approach is especially useful when values arrive incrementally and you do not want to store or reorder the entire dataset each time a new number arrives.
When Sorting Is Still Fine
Although sorting is not asymptotically optimal, it is often acceptable for small arrays or when you also need the full order for another reason. It is also easier to read and less error-prone than a hand-written Quickselect implementation.
If the array is short or the code path is not performance-sensitive, this may be the most sensible choice. The right algorithm is not only about theory. It is about the actual cost in your program, including code clarity and maintenance risk.
Distinct Values Versus Duplicate Values
You also need to define whether "nth biggest" means the nth item in descending order or the nth distinct value. In an array like [9, 9, 8, 7], the second biggest value is either 9 or 8 depending on that definition.
If you need the nth distinct value, remove duplicates first or adapt the algorithm to track distinct numbers:
Being explicit about duplicates prevents subtle bugs and off-by-one arguments later.
Common Pitfalls
One common mistake is sorting the full array in place and accidentally mutating data that other code still needs in its original order. Another is forgetting whether n is one-based or zero-based when translating the problem into an index. Quickselect implementations also often degrade because of poor pivot choice on adversarial data, so production libraries may use more sophisticated pivot strategies. Finally, many bugs come from not defining how duplicates should behave. The nth largest element and the nth distinct largest element are different problems, and the code should reflect that difference clearly.
Summary
- For a one-off selection, Quickselect gives the best average complexity at
O(n). - For streaming data or small
n, a min-heap givesO(n log k)and keeps memory usage low. - Full sorting is simple and acceptable when the array is small or already needs ordering.
- Decide up front whether duplicates count separately or only as distinct values.
- Be careful about index conversion and unintended in-place mutation.

