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..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 @@ -151,6 +151,98 @@ 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 ''; +} +``` + +#### 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 ''; +} +``` + + + + + + + +### 方法二:哈希表 + + + +#### 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 ''; +} +``` + +#### 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 ed9fe534b1c1a..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 @@ -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,98 @@ 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 ''; +} +``` + +#### 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 ''; +} +``` + + + + + + + +### 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 ''; +} +``` + +#### 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/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 ''; +} 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 ''; +} 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 ''; +} 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 ''; +}