Skip to content

Commit ff21319

Browse files
authored
Create Solution3.ts
1 parent cdf624b commit ff21319

File tree

1 file changed

+13
-0
lines changed
  • solution/0200-0299/0215.Kth Largest Element in an Array

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function findKthLargest(nums: number[], k: number): number {
2+
const cnt: Record<number, number> = {};
3+
for (const x of nums) {
4+
cnt[x] = (cnt[x] || 0) + 1;
5+
}
6+
const m = Math.max(...nums);
7+
for (let i = m; ; --i) {
8+
k -= cnt[i] || 0;
9+
if (k <= 0) {
10+
return i;
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)