Problem · Array
Kth Largest Element in an Array
Learn this problemProblem statement
Given an integer array nums and an integer k, return the kth largest element in sorted order.
For this exercise, assume duplicate occurrences count separately; the answer is not the kth distinct value.
Function
findKthLargest(nums: int[], k: int) → intExamples
Example 1
nums = [3,2,1,5,6,4]k = 2return = 5Descending order is [6,5,4,3,2,1], whose second value is 5.
Example 2
nums = [3,2,3,1,2,4,5,5,6]k = 4return = 4The two occurrences of 5 occupy separate ranks, so the fourth largest occurrence is 4.
Constraints
1 <= k <= nums.length <= 10^5.-10^4 <= nums[i] <= 10^4.