Skip to content

Commit 3e6a439

Browse files
authored
feat: add js/ts solutions to lc problem: No.2053 (doocs#3366)
1 parent ff1c11a commit 3e6a439

File tree

6 files changed

+248
-2
lines changed

6 files changed

+248
-2
lines changed

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

+92
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,98 @@ 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+
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+
192+
<!-- tabs:end -->
193+
194+
<!-- solution:end -->
195+
196+
<!-- solution:start -->
197+
198+
### 方法二:哈希表
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+
#### 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+
154246
<!-- tabs:end -->
155247
156248
<!-- solution:end -->

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

+94-2
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,98 @@ 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+
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+
193+
<!-- tabs:end -->
194+
195+
<!-- solution:end -->
196+
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+
#### 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+
155247
<!-- tabs:end -->
156248
157249
<!-- solution:end -->
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+
}
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+
}
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+
}
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)