Problem · Array

Kth Largest Element in an Array

Learn this problem
MediumTokopedia logoTokopediaFULLTIMEONSITE INTERVIEW

Problem statement

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

Duplicate occurrences count separately. The answer is not the kth distinct value.

Function

findKthLargest(nums: int[], k: int) → int

Examples

Example 1

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

Descending order is [6,5,4,3,2,1], so the second element is 5.

Example 2

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

The duplicate fives occupy two positions, and the fourth largest occurrence is 4.

Constraints

  • 1 <= k <= nums.length <= 100000.
  • -10000 <= nums[i] <= 10000.

More Tokopedia problems

drafts saved locally
public int findKthLargest(int[] nums, int k) {
    // Write your code here.
}
nums[3,2,1,5,6,4]
k2
expected5
checking account