LeetCode 2053. Kth Distinct String in an Array — JavaScript

Gary Huang
Dublin so code
Published in
Aug 5, 2024
Intel Disappointed Investors. For Corporate Customers, It’s Still Good Enough.

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

日本山形 HIPHOP街舞 The Feast — ISHONE / FunkyGary / Japan -Yamagata

--

--

Gary Huang
Dublin so code

Self-taught in programming, currently working as a web developer and also a online course instructor, helping self-taught programmers find employment.