Skip to content

Commit f88101f

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

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,36 @@ function kthDistinct(arr k) {
193193
194194
<!-- solution:end -->
195195
196+
<!-- solution:start -->
197+
198+
### Solution 2: Hash Set
199+
200+
<!-- tabs:start -->
201+
202+
#### TypeScript
203+
204+
```ts
205+
function kthDistinct(arr: string[], k: number): string {
206+
const distinct = new Set<string>();
207+
const duplicate = new Set<string>();
208+
209+
for (const x of arr) {
210+
if (distinct.has(x)) {
211+
distinct.delete(x);
212+
duplicate.add(x);
213+
} else if (!duplicate.has(x)) distinct.add(x);
214+
}
215+
216+
for (const x of distinct) {
217+
if (--k === 0) return x;
218+
}
219+
220+
return '';
221+
}
222+
```
223+
224+
<!-- tabs:end -->
225+
226+
<!-- solution:end -->
227+
196228
<!-- problem:end -->

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,36 @@ function kthDistinct(arr k) {
194194
195195
<!-- solution:end -->
196196
197+
<!-- solution:start -->
198+
199+
### Solution 2: Hash Set
200+
201+
<!-- tabs:start -->
202+
203+
#### TypeScript
204+
205+
```ts
206+
function kthDistinct(arr: string[], k: number): string {
207+
const distinct = new Set<string>();
208+
const duplicate = new Set<string>();
209+
210+
for (const x of arr) {
211+
if (distinct.has(x)) {
212+
distinct.delete(x);
213+
duplicate.add(x);
214+
} else if (!duplicate.has(x)) distinct.add(x);
215+
}
216+
217+
for (const x of distinct) {
218+
if (--k === 0) return x;
219+
}
220+
221+
return '';
222+
}
223+
```
224+
225+
<!-- tabs:end -->
226+
227+
<!-- solution:end -->
228+
197229
<!-- problem:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function kthDistinct(arr: string[], k: number): string {
2+
const distinct = new Set<string>();
3+
const duplicate = new Set<string>();
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)