[C++ / Go] Sliding Window + Hash Table - O(n) time + O(m) space solution
Intuition\nNumber of subarrays with exactly k different integers = Number of subarrays with at most k different integers - Number of subarrays with at most k - 
# Intuition\nNumber of subarrays with **exactly** `k` different integers = Number of subarrays with **at most** `k` different integers - Number of subarrays with **at most** `k - 1` different integers \n\n# Approach\n- Count number of subarrays with at most `k` different integers using the sliding window technique. \n    - Maintain a hash table to track the occurences of each different integers.\n    - Maintain a counter for counting number of different integers in the current window.\n- When sliding to position `r`:\n    - Increment the occurence count of `nums[r]`.\n        - If the occurrence count of `nums[r]` is `1`, then `nums[r]` is a new different integer in the current window, increment the counter.\n    - While the number of distinct integers is greater than `k`, slide the left side `l` to remove the begining numbers.\n        - If after removing, the  occurrence count of that number is `0`, then a different integer is removed, decrement the counter.\n    - Add to result with `r - l + 1` subarrays with at most `k` different integers and ending at `r`.\n        - Explain : `differentIntegers[l .. r] <= goal` => `differentIntegers[l + 1 .. r] <= goal` => .... => `differentIntegers[r .. r] <= goal`\n\n# Complexity\n- Time complexity: $O(n)$\n- Space complexity: $O(m)$, `m` is the number of different integers in `nums`\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int subarraysWithAtMostKDistinct(const vector<int>& nums, int k) {\n        if (k == 0)\n            return 0;\n\n        unordered_map<int, int> countOccurence;\n        int differentIntegers = 0;\n        int left = 0;\n        int result = 0;\n        for (int right = 0; right < nums.size(); right++) {\n            countOccurence[nums[right]]++;\n            if (countOccurence[nums[right]] == 1)\n                differentIntegers++;\n\n            while (differentIntegers > k) {\n                countOccurence[nums[left]]--;\n                if (countOccurence[nums[left]] == 0)\n                    differentIntegers--;\n                left++;\n            }\n            \n            result += right - left + 1;\n        }\n        return result;\n    }\n\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n        return subarraysWithAtMostKDistinct(nums, k) - subarraysWithAtMostKDistinct(nums, k - 1);\n    }\n};\n```\n```Go []\nfunc subarraysWithAtMostKDistinct(nums []int, k int) int {\n    if k == 0 {\n        return 0\n    }\n\n    countOccurrence := make(map[int]int)\n    differentIntegers := 0\n    left := 0\n    result := 0\n\n    for right := 0; right < len(nums); right++ {\n        countOccurrence[nums[right]]++\n        if countOccurrence[nums[right]] == 1 {\n            differentIntegers++\n        }\n\n        for differentIntegers > k {\n            countOccurrence[nums[left]]--\n            if countOccurrence[nums[left]] == 0 {\n                differentIntegers--\n            }\n            left++\n        }\n\n        result += right - left + 1\n    }\n    return result\n}\n\nfunc subarraysWithKDistinct(nums []int, k int) int {\n    return subarraysWithAtMostKDistinct(nums, k) - subarraysWithAtMostKDistinct(nums, k-1)\n}\n```
['Hash Table', 'Sliding Window', 'C++', 'Go']