Browse curriculum

Kth Largest Element in an Array

Find the kth largest element in an unsorted array.

Problem Understanding

Return the value that would sit at position k if the integer array nums were sorted from largest to smallest.

Duplicates count separately: in [3,3,2] the 2nd largest is 3, not 2.

Example 1:

  • Input: nums = [3,2,1,5,6,4], k = 2
  • Output: 5

Example 2:

  • Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
  • Output: 4

Algorithm Strategy

We can use a Min-Heap to efficiently find the Kth largest element.

  1. Initialize a Min-Heap: We will maintain a heap of size k.
  2. Iterate through the array: For each number:
    • Push it onto the heap.
    • If the heap size exceeds k, pop the smallest element (root).
  3. Result: After processing all numbers, the heap contains the k largest elements. The smallest of these (the root) is exactly the kth largest element.

Why Min-Heap?
We want to keep the "largest" elements. By popping the smallest element whenever we exceed size K, we discard the "small" numbers and keep the "large" ones.

Interactive Visualization

Push each number onto a min-heap and pop whenever it holds more than k. The tree and its array are the same heap; the root at the end is the kth largest.

Dry Run

Input: nums = [3, 2, 1, 5, 6, 4], k = 2

Step Num Action Heap Size Comment
1 3 Push 3 [3] 1
2 2 Push 2 [2, 3] 2 Min-Heap property (2 < 3)
3 1 Push 1 [1, 3, 2] 3 Size > k (3 > 2), Pop Min (1)
4 - Pop 1 [2, 3] 2 Heap holds largest so far
5 5 Push 5 [2, 3, 5] 3 Size > k, Pop Min (2)
6 - Pop 2 [3, 5] 2
7 6 Push 6 [3, 5, 6] 3 Size > k, Pop Min (3)
8 - Pop 3 [5, 6] 2
9 4 Push 4 [4, 6, 5] 3 Size > k, Pop Min (4)
10 - Pop 4 [5, 6] 2
End - Root is Ans 5 - 5 is the 2nd largest

Edge Cases

  • k = 1: the heap keeps one value, the maximum.
  • k = n: nothing is ever popped, and the root is the minimum.
  • Duplicates: each copy counts. In [3, 2, 3, 1, 2, 4, 5, 5, 6] with k = 4 the answer is 4 — both 5s take a place.
  • Negative numbers: nothing changes; the heap compares values, not magnitudes.

Solution

A min-heap of size k in every language. JavaScript, TypeScript and C have no built-in heap, so their tabs carry a small one.

Kth Largest Element in an Array Code
1class Heap {
2 constructor(before) {
3 this.items = [];
4 this.before = before;
5 }
6
7 size() {
8 return this.items.length;
9 }
10
11 peek() {
12 return this.items[0];
13 }
14
15 push(item) {
16 const a = this.items;
17 a.push(item);
18 let i = a.length - 1;
19 while (i > 0 && this.before(a[i], a[(i - 1) >> 1])) {
20 const parent = (i - 1) >> 1;
21 [a[i], a[parent]] = [a[parent], a[i]];
22 i = parent;
23 }
24 }
25
26 pop() {
27 const a = this.items;
28 const top = a[0];
29 const last = a.pop();
30 if (a.length > 0) {
31 a[0] = last;
32 let i = 0;
33 while (true) {
34 const left = 2 * i + 1, right = 2 * i + 2;
35 let first = i;
36 if (left < a.length && this.before(a[left], a[first])) first = left;
37 if (right < a.length && this.before(a[right], a[first])) first = right;
38 if (first === i) break;
39 [a[i], a[first]] = [a[first], a[i]];
40 i = first;
41 }
42 }
43 return top;
44 }
45}
46
47var findKthLargest = function(nums, k) {
48 const heap = new Heap((a, b) => a < b);
49 for (const num of nums) {
50 heap.push(num);
51 if (heap.size() > k) {
52 heap.pop();
53 }
54 }
55 return heap.peek();
56};

The approach, step by step

  1. Push

    Push each number onto a min-heap.

  2. Trim to k

    If the heap now holds more than k values, pop its minimum.

  3. Return the root

    After the last number, the root of the heap is the kth largest.

Frequently asked questions

Why use a min-heap to find the kth largest?

The heap keeps the k largest values seen so far. Its minimum is the weakest of them, so it is the one to throw out when a larger value arrives — and after the last number it is exactly the kth largest.


What is the time complexity?

O(n log k): each of the n numbers is pushed, and at most one pop follows, on a heap of at most k + 1 values. The heap uses O(k) space.


Do duplicate values count?

Yes. The problem asks for the kth largest in sorted order, not the kth distinct value, so equal numbers each take a place in the heap.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description