From 68d1ba3fff6bcc1ab5a12931c56b620ce47c344b Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 5 Aug 2024 22:07:45 +0200 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB2053.=20Kth=20Distinct=20Stri?= =?UTF-8?q?ng=20in=20an=20Array=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/kth-distinct-string-in-an-array.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 cs/kth-distinct-string-in-an-array.cs diff --git a/cs/kth-distinct-string-in-an-array.cs b/cs/kth-distinct-string-in-an-array.cs new file mode 100644 index 0000000..d08a5d5 --- /dev/null +++ b/cs/kth-distinct-string-in-an-array.cs @@ -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]; + } +}