cs: add «2053. Kth Distinct String in an Array»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-05 22:07:45 +02:00
parent d36f1554da
commit 68d1ba3fff
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,21 @@
public class Solution {
public string KthDistinct(string[] arr, int k) {
var uniqueWords = arr.ToHashSet();
var freqs = uniqueWords.ToDictionary(key => key, key => arr.Count(w => w == key));
var (i, found) = (0, 0);
for (; i < arr.Length && found < k; ++i) {
if (freqs[arr[i]] != 1) {
continue;
}
++found;
}
if (found < k) {
return "";
}
return arr[i - 1];
}
}