Kth Distinct String in an Array LeetCode solution in JavaScript
1var kthDistinct = function (arr, k) {2const map = {}; // used for arr occurences3const distinctArr = []; // store the distinct values (only appearing once)45// increment the occurence to the map6arr.forEach((letter) => (map[letter] = map[letter] + 1 || 1));78// store all the distinct values in order9for (let [key, val] of Object.entries(map))10if (val == 1) distinctArr.push(key);1112// return the key or empty string13return distinctArr[k - 1] || "";14};15~``;