Search⌘ K
AI Features

Kth Largest Element in an Array

Explore techniques to identify the kth largest element in an unsorted array. Understand problem constraints and implement solutions efficiently using JavaScript in a hands-on environment.

Statement

Given an integer array, nums, and an integer, k, determine and return the kth largest element in the array.

Note: The kth largest element is defined with respect to the array’s sorted order (descending), and does not necessarily correspond to the kth unique value.

Constraints:

  • 11 \leq k \leq nums.length 103\leq 10^3

  • 104-10^4 \leq nums[i] 104\leq 10^4

Examples

canvasAnimation-image
1 / 4

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Kth Largest Element in an Array

1.

What is the 4th largest element in the following unsorted array?

[5, 12, 9, 0, 6, 7, 1, 8, 4, 9]

A.

9

B.

7

C.

8

D.

6


1 / 4

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4

Try it yourself

Implement your solution in main.js in the following coding playground:

JavaScript
usercode > Solution.js
import { MinHeap, MaxHeap } from './Heap.js'
/* The following definition is for MinHeap.
You can use the same methods for MaxHeap.
class MinHeap {
size(); // return number of elements
peek(); // return top element without removing
push(val); // insert element
pop(); // remove and return top element
}
*/
function findKthLargest(nums, k){
// Replace this placeholder return statement with your code
return -1
}
export {
findKthLargest
}
Kth Largest Element in an Array