File tree Expand file tree Collapse file tree 3 files changed +81
-0
lines changed
solution/2000-2099/2053.Kth Distinct String in an Array Expand file tree Collapse file tree 3 files changed +81
-0
lines changed Original file line number Diff line number Diff line change @@ -193,4 +193,36 @@ function kthDistinct(arr k) {
193
193
194
194
<!-- solution:end -->
195
195
196
+ <!-- solution:start -->
197
+
198
+ ### Solution 2: Hash Set
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
+ <!-- tabs:end -->
225
+
226
+ <!-- solution:end -->
227
+
196
228
<!-- problem:end -->
Original file line number Diff line number Diff line change @@ -194,4 +194,36 @@ function kthDistinct(arr k) {
194
194
195
195
<!-- solution:end -->
196
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
+ <!-- tabs:end -->
226
+
227
+ <!-- solution:end -->
228
+
197
229
<!-- problem:end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments