C# | Runtime 181 ms beats 77.42%, Memory 53.36 MB beats 66.13% [EXPLAINED]
Intuition\nTo find subarrays with exactly k different integers, I use the idea that subarrays with exactly k can be found by subtracting subarrays with at most 
# Intuition\nTo find subarrays with exactly k different integers, I use the idea that subarrays with exactly k can be found by subtracting subarrays with at most k-1 from those with at most k.\n\n# Approach\nCount Subarrays with At Most k Distinct Integers: Use a sliding window technique to track subarrays with up to k distinct integers.\n\nSubtract to Find Exactly k: Subtract the count of subarrays with at most k-1 distinct integers from those with at most k.\n\n# Complexity\n- Time complexity:\nO(n) \u2013 each element is processed at most twice.\n\n- Space complexity:\nO(n) \u2013 for storing counts in a frequency dictionary.\n\n# Code\n```csharp []\npublic class Solution {\n    public int SubarraysWithKDistinct(int[] nums, int k) {\n        return AtMostKDistinct(nums, k) - AtMostKDistinct(nums, k - 1);\n    }\n    \n    private int AtMostKDistinct(int[] nums, int k) {\n        if (k == 0) return 0;\n\n        var count = 0;\n        var frequency = new Dictionary<int, int>();\n        int left = 0;\n\n        for (int right = 0; right < nums.Length; right++) {\n            if (!frequency.ContainsKey(nums[right])) {\n                frequency[nums[right]] = 0;\n            }\n            frequency[nums[right]]++;\n\n            while (frequency.Count > k) {\n                frequency[nums[left]]--;\n                if (frequency[nums[left]] == 0) {\n                    frequency.Remove(nums[left]);\n                }\n                left++;\n            }\n\n            count += right - left + 1;\n        }\n\n        return count;\n    }\n}\n\n```
['C#']