Skip to content

Commit ae4efe7

Browse files
committed
feat: add js solution to lc problem: No.2053
1 parent f88101f commit ae4efe7

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ function kthDistinct(arr: string[], k: number): string {
221221
}
222222
```
223223
224+
#### JavaScript
225+
226+
```js
227+
function kthDistinct(arr, k) {
228+
const distinct = new Set();
229+
const duplicate = new Set();
230+
231+
for (const x of arr) {
232+
if (distinct.has(x)) {
233+
distinct.delete(x);
234+
duplicate.add(x);
235+
} else if (!duplicate.has(x)) distinct.add(x);
236+
}
237+
238+
for (const x of distinct) {
239+
if (--k === 0) return x;
240+
}
241+
242+
return '';
243+
}
244+
```
245+
224246
<!-- tabs:end -->
225247
226248
<!-- solution:end -->

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,28 @@ function kthDistinct(arr: string[], k: number): string {
222222
}
223223
```
224224
225+
#### JavaScript
226+
227+
```js
228+
function kthDistinct(arr, k) {
229+
const distinct = new Set();
230+
const duplicate = new Set();
231+
232+
for (const x of arr) {
233+
if (distinct.has(x)) {
234+
distinct.delete(x);
235+
duplicate.add(x);
236+
} else if (!duplicate.has(x)) distinct.add(x);
237+
}
238+
239+
for (const x of distinct) {
240+
if (--k === 0) return x;
241+
}
242+
243+
return '';
244+
}
245+
```
246+
225247
<!-- tabs:end -->
226248
227249
<!-- solution:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function kthDistinct(arr, k) {
2+
const distinct = new Set();
3+
const duplicate = new Set();
4+
5+
for (const x of arr) {
6+
if (distinct.has(x)) {
7+
distinct.delete(x);
8+
duplicate.add(x);
9+
} else if (!duplicate.has(x)) distinct.add(x);
10+
}
11+
12+
for (const x of distinct) {
13+
if (--k === 0) return x;
14+
}
15+
16+
return '';
17+
}

0 commit comments

Comments
 (0)