algorithms
array sorting
nth largest element
programming techniques
computational efficiency

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.

python
1def nth_largest(nums, n):
2    target = len(nums) - n
3    left, right = 0, len(nums) - 1
4
5    while True:
6        pivot = nums[right]
7        store = left
8
9        for i in range(left, right):
10            if nums[i] < pivot:
11                nums[store], nums[i] = nums[i], nums[store]
12                store += 1
13
14        nums[store], nums[right] = nums[right], nums[store]
15
16        if store == target:
17            return nums[store]
18        if store < target:
19            left = store + 1
20        else:
21            right = store - 1
22
23data = [7, 2, 9, 4, 3, 8, 6]
24print(nth_largest(data[:], 3))

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.

python
1import heapq
2
3def nth_largest_heap(nums, n):
4    heap = []
5    for value in nums:
6        if len(heap) < n:
7            heapq.heappush(heap, value)
8        elif value > heap[0]:
9            heapq.heapreplace(heap, value)
10    return heap[0]
11
12data = [7, 2, 9, 4, 3, 8, 6]
13print(nth_largest_heap(data, 3))

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.

python
def nth_largest_sort(nums, n):
    return sorted(nums)[-n]

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:

python
1def nth_distinct_largest(nums, n):
2    unique = sorted(set(nums))
3    return unique[-n]
4
5print(nth_distinct_largest([9, 9, 8, 7], 2))

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 gives O(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.

Course illustration
Course illustration

All Rights Reserved.