Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Kth Largest Element

Sortingmedium

Course · Section 11: Working With Arrays · Lecture 172: Sorting Arrays

Implement `findKthLargest(nums, k)` returning the k-th largest element in `nums`.

Sample tests

Input: findKthLargest([3,2,1,5,6,4], 2)
Output: 5
Input: findKthLargest([3,2,3,1,2,4,5,5,6], 4)
Output: 4

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Off-by-one between k and the zero-based index.

Learning resources

  • MDN: Array.prototype.sort
Approach & explanation (try first)

Sorting descending and indexing k-1 is O(n log n); quickselect would be O(n) average.

Loading...
⌘/Ctrl + Enter

Run your code to see results.