Skip to content

Commit

Permalink
Sync LeetCode submission - Kth Largest Element in an Array (javascript)
Browse files Browse the repository at this point in the history
  • Loading branch information
DhanushNehru committed Aug 14, 2023
1 parent 92d28f0 commit 720f831
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions problems/kth_largest_element_in_an_array/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var findKthLargest = function(nums, k) {
// Create a new Min Priority Queue to store the k largest elements.
let pq = new MinPriorityQueue();

// Iterate through each element 'x' in the 'nums' array.
for (let x of nums) {
// Enqueue the current element 'x' into the priority queue.
pq.enqueue(x);

// If the size of the priority queue becomes larger than 'k', remove the smallest element.
if (pq.size() > k) {
pq.dequeue();
}
}

// Return the element at the front of the priority queue, which is the kth largest element.
return pq.front().element;
};

0 comments on commit 720f831

Please sign in to comment.