|
37 | 37 | The only distinct strings in arr are "d" and "a".
|
38 | 38 | "d" appears 1<sup>st</sup>, so it is the 1<sup>st</sup> distinct string.
|
39 | 39 | "a" appears 2<sup>nd</sup>, so it is the 2<sup>nd</sup> distinct string.
|
40 |
| -Since k == 2, "a" is returned. |
| 40 | +Since k == 2, "a" is returned. |
41 | 41 | </pre>
|
42 | 42 |
|
43 | 43 | <p><strong class="example">Example 2:</strong></p>
|
@@ -73,7 +73,7 @@ The only distinct string is "b". Since there are fewer than 3 distinct
|
73 | 73 |
|
74 | 74 | <!-- solution:start -->
|
75 | 75 |
|
76 |
| -### Solution 1 |
| 76 | +### Solution 1: Counting |
77 | 77 |
|
78 | 78 | <!-- tabs:start -->
|
79 | 79 |
|
@@ -152,6 +152,98 @@ func kthDistinct(arr []string, k int) string {
|
152 | 152 | }
|
153 | 153 | ```
|
154 | 154 |
|
| 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 | +
|
155 | 247 | <!-- tabs:end -->
|
156 | 248 |
|
157 | 249 | <!-- solution:end -->
|
|
0 commit comments