🔥C++ Solution ||  Using Hashmaps || Faster and Easy to understand
\n# Code\n\nclass Solution {\npublic:\n    int atMostK(vector<int>&nums,int k){\n        map<int,int> mp;\n        int i=0,ans = 0;\n        for(int j=0;j<nums.
\n# Code\n```\nclass Solution {\npublic:\n    int atMostK(vector<int>&nums,int k){\n        map<int,int> mp;\n        int i=0,ans = 0;\n        for(int j=0;j<nums.size();j++){\n            mp[nums[j]]++;\n\n            while(mp.size() > k){\n                mp[nums[i]]--;\n                if(mp[nums[i]] == 0) mp.erase(nums[i]);\n                i++;\n            }\n\n            ans += (j - i + 1);\n        }\n\n        return ans;\n    }\n\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n      return atMostK(nums,k)  - atMostK(nums,k-1);\n    }\n};\n```
['Array', 'Hash Table', 'Sliding Window', 'C++']