Java Solution Easy!!
\nclass Solution {\n    public int subarraysWithKDistinct(int[] nums, int k) {\n        return atmostK(nums,k)-atmostK(nums,k-1);\n        \n    }\n    public i
```\nclass Solution {\n    public int subarraysWithKDistinct(int[] nums, int k) {\n        return atmostK(nums,k)-atmostK(nums,k-1);\n        \n    }\n    public int atmostK(int []nums,int k)\n    { int ans=0;\n        HashMap<Integer,Integer> mp=new HashMap<>();\n        int i=0; int j=0;\n        while(j<nums.length)\n        {\n            mp.put(nums[j],mp.getOrDefault(nums[j],0)+1);\n            while(mp.size()==k+1)\n            {\n                mp.put(nums[i],mp.get(nums[i])-1);\n                if(mp.get(nums[i])==0)\n                    mp.remove(nums[i]);\n                i++;\n            }\n            ans+=j-i+1;\n            j++;\n            \n        }\n     return ans;\n    }\n}\n```
['Java']