From 95287633bfd2e9041b759fb91f28bb7c117417be Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 5 Aug 2024 15:15:10 +0300 Subject: [PATCH 1/5] feat: add ts solution to lc problem: No.2053 --- .../README.md | 19 +++++++++++++++ .../README_EN.md | 23 +++++++++++++++++-- .../Solution.ts | 14 +++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 solution/2000-2099/2053.Kth Distinct String in an Array/Solution.ts diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/README.md b/solution/2000-2099/2053.Kth Distinct String in an Array/README.md index 4c1ed23414feb..42ca799150189 100644 --- a/solution/2000-2099/2053.Kth Distinct String in an Array/README.md +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/README.md @@ -151,6 +151,25 @@ func kthDistinct(arr []string, k int) string { } ``` +#### TypeScript + +```ts +function kthDistinct(arr: string[], k: number): string { + 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 ''; +} +``` + diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md b/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md index ed9fe534b1c1a..f120a71738956 100644 --- a/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md @@ -37,7 +37,7 @@ tags: The only distinct strings in arr are "d" and "a". "d" appears 1st, so it is the 1st distinct string. "a" appears 2nd, so it is the 2nd distinct string. -Since k == 2, "a" is returned. +Since k == 2, "a" is returned.

Example 2:

@@ -73,7 +73,7 @@ The only distinct string is "b". Since there are fewer than 3 distinct -### Solution 1 +### Solution 1: Counting @@ -152,6 +152,25 @@ func kthDistinct(arr []string, k int) string { } ``` +#### TypeScript + +```ts +function kthDistinct(arr: string[], k: number): string { + 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 ''; +} +``` + diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/Solution.ts b/solution/2000-2099/2053.Kth Distinct String in an Array/Solution.ts new file mode 100644 index 0000000000000..a86d510f48c60 --- /dev/null +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/Solution.ts @@ -0,0 +1,14 @@ +function kthDistinct(arr: string[], k: number): string { + 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 ''; +} From 919c44785da6b9e90293758f42dc0bd7f1bfd6c6 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 5 Aug 2024 15:16:37 +0300 Subject: [PATCH 2/5] feat: add js solution to lc problem: No.2053 --- .../README.md | 19 +++++++++++++++++++ .../README_EN.md | 19 +++++++++++++++++++ .../Solution.js | 14 ++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 solution/2000-2099/2053.Kth Distinct String in an Array/Solution.js diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/README.md b/solution/2000-2099/2053.Kth Distinct String in an Array/README.md index 42ca799150189..836625066487e 100644 --- a/solution/2000-2099/2053.Kth Distinct String in an Array/README.md +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/README.md @@ -170,6 +170,25 @@ function kthDistinct(arr: string[], k: number): string { } ``` +#### 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 ''; +} +``` + diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md b/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md index f120a71738956..2c991f4845ea6 100644 --- a/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md @@ -171,6 +171,25 @@ function kthDistinct(arr: string[], k: number): string { } ``` +#### 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 ''; +} +``` + diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/Solution.js b/solution/2000-2099/2053.Kth Distinct String in an Array/Solution.js new file mode 100644 index 0000000000000..dfd538eba9e50 --- /dev/null +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/Solution.js @@ -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 ''; +} From f88101fa30fbe590167000e5222c25ed362fd346 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 5 Aug 2024 15:19:55 +0300 Subject: [PATCH 3/5] feat: add ts solution to lc problem: No.2053 --- .../README.md | 32 +++++++++++++++++++ .../README_EN.md | 32 +++++++++++++++++++ .../Solution2.ts | 17 ++++++++++ 3 files changed, 81 insertions(+) create mode 100644 solution/2000-2099/2053.Kth Distinct String in an Array/Solution2.ts diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/README.md b/solution/2000-2099/2053.Kth Distinct String in an Array/README.md index 836625066487e..85de47b8cfcdc 100644 --- a/solution/2000-2099/2053.Kth Distinct String in an Array/README.md +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/README.md @@ -193,4 +193,36 @@ function kthDistinct(arr k) { + + +### Solution 2: Hash Set + + + +#### TypeScript + +```ts +function kthDistinct(arr: string[], k: number): string { + 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 ''; +} +``` + + + + + diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md b/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md index 2c991f4845ea6..25b7b72b46074 100644 --- a/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md @@ -194,4 +194,36 @@ function kthDistinct(arr k) { + + +### Solution 2: Hash Set + + + +#### TypeScript + +```ts +function kthDistinct(arr: string[], k: number): string { + 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 ''; +} +``` + + + + + diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/Solution2.ts b/solution/2000-2099/2053.Kth Distinct String in an Array/Solution2.ts new file mode 100644 index 0000000000000..25879beb54924 --- /dev/null +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/Solution2.ts @@ -0,0 +1,17 @@ +function kthDistinct(arr: string[], k: number): string { + 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 ''; +} From ae4efe7db0c7eb5c1cb39f0f94735a714ff6ae4f Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 5 Aug 2024 15:21:24 +0300 Subject: [PATCH 4/5] feat: add js solution to lc problem: No.2053 --- .../README.md | 22 +++++++++++++++++++ .../README_EN.md | 22 +++++++++++++++++++ .../Solution2.js | 17 ++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 solution/2000-2099/2053.Kth Distinct String in an Array/Solution2.js diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/README.md b/solution/2000-2099/2053.Kth Distinct String in an Array/README.md index 85de47b8cfcdc..99d5cf7d3b758 100644 --- a/solution/2000-2099/2053.Kth Distinct String in an Array/README.md +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/README.md @@ -221,6 +221,28 @@ function kthDistinct(arr: string[], k: number): string { } ``` +#### 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 ''; +} +``` + diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md b/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md index 25b7b72b46074..87e769117b372 100644 --- a/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/README_EN.md @@ -222,6 +222,28 @@ function kthDistinct(arr: string[], k: number): string { } ``` +#### 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 ''; +} +``` + diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/Solution2.js b/solution/2000-2099/2053.Kth Distinct String in an Array/Solution2.js new file mode 100644 index 0000000000000..ac6ce8b3e0750 --- /dev/null +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/Solution2.js @@ -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 ''; +} From 19744fb8fc48a853b5ad6217b79cffa0e32ca7f8 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 6 Aug 2024 08:12:49 +0800 Subject: [PATCH 5/5] Update README.md --- .../2000-2099/2053.Kth Distinct String in an Array/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/README.md b/solution/2000-2099/2053.Kth Distinct String in an Array/README.md index 99d5cf7d3b758..c3b4767aeec08 100644 --- a/solution/2000-2099/2053.Kth Distinct String in an Array/README.md +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/README.md @@ -195,7 +195,7 @@ function kthDistinct(arr k) { -### Solution 2: Hash Set +### 方法二:哈希表