Kth Largest Element in an Array - aloalgo

Kth Largest Element in an Array

Medium

You are given an integer array nums and an integer k.

Your task is to find and return the kth largest element in the array.

You may assume that:

  • The array nums is non-empty.
  • k is a valid integer such that 1 <= k <= nums.length.

Return the kth largest element as an integer.

Example 1

Inputs
nums = [3, 1, 2]
k = 2
Output
2
Explanation:

The largest element is 3, the 2nd largest is 2.

Example 2

Inputs
nums = [1, 3, 2, 1, 3]
k = 3
Output
2
Explanation:

The largest element is 3, the 2nd largest is also 3, and the 3rd largest is 2.

Loading...
Inputs
nums = [3, 1, 2]
k = 2
Output
2

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!