Skip to content

Commit 008a174

Browse files
committed
Add solution #2053
1 parent 2b34b1c commit 008a174

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
2011|[Final Value of Variable After Performing Operations](./2011-final-value-of-variable-after-performing-operations.js)|Easy|
245245
2016|[Maximum Difference Between Increasing Elements](./2016-maximum-difference-between-increasing-elements.js)|Easy|
246246
2047|[Number of Valid Words in a Sentence](./2047-number-of-valid-words-in-a-sentence.js)|Easy|
247+
2053|[Kth Distinct String in an Array](./2053-kth-distinct-string-in-an-array.js)|Medium|
247248
2095|[Delete the Middle Node of a Linked List](./2095-delete-the-middle-node-of-a-linked-list.js)|Medium|
248249
2114|[Maximum Number of Words Found in Sentences](./2114-maximum-number-of-words-found-in-sentences.js)|Easy|
249250

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 2053. Kth Distinct String in an Array
3+
* https://leetcode.com/problems/kth-distinct-string-in-an-array/
4+
* Difficulty: Medium
5+
*
6+
* A distinct string is a string that is present only once in an array.
7+
*
8+
* Given an array of strings arr, and an integer k, return the kth distinct
9+
* string present in arr. If there are fewer than k distinct strings, return
10+
* an empty string "".
11+
*
12+
* Note that the strings are considered in the order in which they appear in
13+
* the array.
14+
*/
15+
16+
/**
17+
* @param {string[]} arr
18+
* @param {number} k
19+
* @return {string}
20+
*/
21+
var kthDistinct = function(arr, k) {
22+
const map = new Map();
23+
arr.forEach(n => map.set(n, (map.get(n) || 0) + 1));
24+
25+
const sorted = [...map].sort(([,a], [,b]) => a - b);
26+
const [kth, count] = sorted[k - 1] || [];
27+
28+
return count === 1 ? kth : '';
29+
};

0 commit comments

Comments
 (0)