Skip to content

Commit 9528763

Browse files
committed
feat: add ts solution to lc problem: No.2053
1 parent ff1c11a commit 9528763

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
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
@@ -151,6 +151,25 @@ func kthDistinct(arr []string, k int) string {
151151
}
152152
```
153153

154+
#### TypeScript
155+
156+
```ts
157+
function kthDistinct(arr: string[], k: number): string {
158+
const cnt = new Map<string, number>();
159+
160+
for (const x of arr) {
161+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
162+
}
163+
164+
for (const [x, c] of cnt) {
165+
if (c === 1) k--;
166+
if (!k) return x;
167+
}
168+
169+
return '';
170+
}
171+
```
172+
154173
<!-- tabs:end -->
155174

156175
<!-- solution:end -->

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tags:
3737
The only distinct strings in arr are &quot;d&quot; and &quot;a&quot;.
3838
&quot;d&quot; appears 1<sup>st</sup>, so it is the 1<sup>st</sup> distinct string.
3939
&quot;a&quot; appears 2<sup>nd</sup>, so it is the 2<sup>nd</sup> distinct string.
40-
Since k == 2, &quot;a&quot; is returned.
40+
Since k == 2, &quot;a&quot; is returned.
4141
</pre>
4242

4343
<p><strong class="example">Example 2:</strong></p>
@@ -73,7 +73,7 @@ The only distinct string is &quot;b&quot;. Since there are fewer than 3 distinct
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Counting
7777

7878
<!-- tabs:start -->
7979

@@ -152,6 +152,25 @@ func kthDistinct(arr []string, k int) string {
152152
}
153153
```
154154

155+
#### TypeScript
156+
157+
```ts
158+
function kthDistinct(arr: string[], k: number): string {
159+
const cnt = new Map<string, number>();
160+
161+
for (const x of arr) {
162+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
163+
}
164+
165+
for (const [x, c] of cnt) {
166+
if (c === 1) k--;
167+
if (!k) return x;
168+
}
169+
170+
return '';
171+
}
172+
```
173+
155174
<!-- tabs:end -->
156175

157176
<!-- solution:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function kthDistinct(arr: string[], k: number): string {
2+
const cnt = new Map<string, number>();
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)