LeetCode 2053. Kth Distinct String in an Array — JavaScript
Published in
Aug 5, 2024
https://leetcode.com/problems/kth-distinct-string-in-an-array/description/
領500元現金了嗎? 點選我的連結成功開樂天帳戶+登入樂天網銀APP,拿500元現金! 樂天帳戶好康在這 : · 使用行動支付5次,享次月活儲年息1.35%存額無上限 · VIP享每月免費跨提/轉共16次 · 提領日幣手續費優惠8次/月 (推薦序號: JGONGL)
var kthDistinct = function(arr, k) {
let map = new Map(), ans = ""
arr.forEach(e=>{
map.has(e) ? map.set(e,map.get(e)+1) : map.set(e,1)
})
map.forEach((value,key)=>{
if(value===1) {
k-=1
if(k===0) ans = key
}
})
return ans
};
using the JS Map to find out the k distinct string