1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00
LeetCode/cs/kth-distinct-string-in-an-array.cs
Matej Focko 68d1ba3fff
cs: add «2053. Kth Distinct String in an Array»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-08-05 22:07:45 +02:00

21 lines
501 B
C#

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];
}
}