Java Sliding Window Easy Consise
\nclass Solution {\n    // number of subarrays with k diff integers = number of subarrays with at most k diff integers - number of subarrays with atmost k-1 dif
```\nclass Solution {\n    // number of subarrays with k diff integers = number of subarrays with at most k diff integers - number of subarrays with atmost k-1 diff integers\n    public int subarraysWithKDistinct(int[] nums, int k) {\n        return count(nums,k) - count(nums,k-1);\n    }\n    public int count(int[] nums,int k){\n        HashMap<Integer,Integer> map = new HashMap<>();\n        int start = 0,end = 0, res = 0;\n        while(end < nums.length){\n            map.put(nums[end],map.getOrDefault(nums[end],0)+1);\n            while(start <= end && map.size() > k){\n                if(map.containsKey(nums[start])) map.put(nums[start],map.get(nums[start])-1);\n                if(map.get(nums[start]) == 0) map.remove(nums[start]);\n                start++;\n            }\n            res+= (end - start + 1);\n            end++;\n        }\n        return res;\n    }\n}\n```
['Java']