Kth Distinct String in an Array LeetCode solution in JavaScript

1
var kthDistinct = function (arr, k) {
2
const map = {}; // used for arr occurences
3
const distinctArr = []; // store the distinct values (only appearing once)
4

5
// increment the occurence to the map
6
arr.forEach((letter) => (map[letter] = map[letter] + 1 || 1));
7

8
// store all the distinct values in order
9
for (let [key, val] of Object.entries(map))
10
if (val == 1) distinctArr.push(key);
11

12
// return the key or empty string
13
return distinctArr[k - 1] || "";
14
};
15
~``;

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0