Skip to content

Commit 919c447

Browse files
committed
feat: add js solution to lc problem: No.2053
1 parent 9528763 commit 919c447

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

solution/2000-2099/2053.Kth Distinct String in an Array/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@ function kthDistinct(arr: string[], k: number): string {
170170
}
171171
```
172172

173+
#### JavaScript
174+
175+
```js
176+
function kthDistinct(arr k) {
177+
const cnt = new Map();
178+
179+
for (const x of arr) {
180+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
181+
}
182+
183+
for (const [x, c] of cnt) {
184+
if (c === 1) k--;
185+
if (!k) return x;
186+
}
187+
188+
return '';
189+
}
190+
```
191+
173192
<!-- tabs:end -->
174193
175194
<!-- solution:end -->

solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,25 @@ function kthDistinct(arr: string[], k: number): string {
171171
}
172172
```
173173

174+
#### JavaScript
175+
176+
```js
177+
function kthDistinct(arr k) {
178+
const cnt = new Map();
179+
180+
for (const x of arr) {
181+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
182+
}
183+
184+
for (const [x, c] of cnt) {
185+
if (c === 1) k--;
186+
if (!k) return x;
187+
}
188+
189+
return '';
190+
}
191+
```
192+
174193
<!-- tabs:end -->
175194
176195
<!-- solution:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function kthDistinct(arr k) {
2+
const cnt = new Map();
3+
4+
for (const x of arr) {
5+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
6+
}
7+
8+
for (const [x, c] of cnt) {
9+
if (c === 1) k--;
10+
if (!k) return x;
11+
}
12+
13+
return '';
14+
}

0 commit comments

Comments
 (0)