Skip to content

feat: add js/ts solutions to lc problem: No.2053 #3366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions solution/2000-2099/2053.Kth Distinct String in an Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,98 @@ func kthDistinct(arr []string, k int) string {
}
```

#### TypeScript

```ts
function kthDistinct(arr: string[], k: number): string {
const cnt = new Map<string, number>();

for (const x of arr) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}

for (const [x, c] of cnt) {
if (c === 1) k--;
if (!k) return x;
}

return '';
}
```

#### JavaScript

```js
function kthDistinct(arr k) {
const cnt = new Map();

for (const x of arr) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}

for (const [x, c] of cnt) {
if (c === 1) k--;
if (!k) return x;
}

return '';
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二:哈希表

<!-- tabs:start -->

#### TypeScript

```ts
function kthDistinct(arr: string[], k: number): string {
const distinct = new Set<string>();
const duplicate = new Set<string>();

for (const x of arr) {
if (distinct.has(x)) {
distinct.delete(x);
duplicate.add(x);
} else if (!duplicate.has(x)) distinct.add(x);
}

for (const x of distinct) {
if (--k === 0) return x;
}

return '';
}
```

#### JavaScript

```js
function kthDistinct(arr, k) {
const distinct = new Set();
const duplicate = new Set();

for (const x of arr) {
if (distinct.has(x)) {
distinct.delete(x);
duplicate.add(x);
} else if (!duplicate.has(x)) distinct.add(x);
}

for (const x of distinct) {
if (--k === 0) return x;
}

return '';
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tags:
The only distinct strings in arr are &quot;d&quot; and &quot;a&quot;.
&quot;d&quot; appears 1<sup>st</sup>, so it is the 1<sup>st</sup> distinct string.
&quot;a&quot; appears 2<sup>nd</sup>, so it is the 2<sup>nd</sup> distinct string.
Since k == 2, &quot;a&quot; is returned.
Since k == 2, &quot;a&quot; is returned.
</pre>

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

<!-- solution:start -->

### Solution 1
### Solution 1: Counting

<!-- tabs:start -->

Expand Down Expand Up @@ -152,6 +152,98 @@ func kthDistinct(arr []string, k int) string {
}
```

#### TypeScript

```ts
function kthDistinct(arr: string[], k: number): string {
const cnt = new Map<string, number>();

for (const x of arr) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}

for (const [x, c] of cnt) {
if (c === 1) k--;
if (!k) return x;
}

return '';
}
```

#### JavaScript

```js
function kthDistinct(arr k) {
const cnt = new Map();

for (const x of arr) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}

for (const [x, c] of cnt) {
if (c === 1) k--;
if (!k) return x;
}

return '';
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: Hash Set

<!-- tabs:start -->

#### TypeScript

```ts
function kthDistinct(arr: string[], k: number): string {
const distinct = new Set<string>();
const duplicate = new Set<string>();

for (const x of arr) {
if (distinct.has(x)) {
distinct.delete(x);
duplicate.add(x);
} else if (!duplicate.has(x)) distinct.add(x);
}

for (const x of distinct) {
if (--k === 0) return x;
}

return '';
}
```

#### JavaScript

```js
function kthDistinct(arr, k) {
const distinct = new Set();
const duplicate = new Set();

for (const x of arr) {
if (distinct.has(x)) {
distinct.delete(x);
duplicate.add(x);
} else if (!duplicate.has(x)) distinct.add(x);
}

for (const x of distinct) {
if (--k === 0) return x;
}

return '';
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function kthDistinct(arr k) {
const cnt = new Map();

for (const x of arr) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}

for (const [x, c] of cnt) {
if (c === 1) k--;
if (!k) return x;
}

return '';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function kthDistinct(arr: string[], k: number): string {
const cnt = new Map<string, number>();

for (const x of arr) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}

for (const [x, c] of cnt) {
if (c === 1) k--;
if (!k) return x;
}

return '';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function kthDistinct(arr, k) {
const distinct = new Set();
const duplicate = new Set();

for (const x of arr) {
if (distinct.has(x)) {
distinct.delete(x);
duplicate.add(x);
} else if (!duplicate.has(x)) distinct.add(x);
}

for (const x of distinct) {
if (--k === 0) return x;
}

return '';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function kthDistinct(arr: string[], k: number): string {
const distinct = new Set<string>();
const duplicate = new Set<string>();

for (const x of arr) {
if (distinct.has(x)) {
distinct.delete(x);
duplicate.add(x);
} else if (!duplicate.has(x)) distinct.add(x);
}

for (const x of distinct) {
if (--k === 0) return x;
}

return '';
}
Loading