JS
Code\n\nvar subarraysWithKDistinct = function (nums, k) {\n    let n = nums.length;\n    let m = new Array(n + 1).fill(0);\n    let unique = 0;\n    let Left = 
# Code\n```\nvar subarraysWithKDistinct = function (nums, k) {\n    let n = nums.length;\n    let m = new Array(n + 1).fill(0);\n    let unique = 0;\n    let Left = 0;\n    let x = 0;\n\n    let left = 0;\n    for (let num of nums) {\n        if (m[num] === 0) {\n            unique++;\n            if (unique > k) {\n                m[nums[left]]--;\n                unique--;\n                left++;\n                Left = 0;\n            }\n        }\n        m[num]++;\n        while (m[nums[left]] > 1) {\n            m[nums[left]]--;\n            left++;\n            Left++;\n        }\n        if (unique === k) {\n            x += Left + 1;\n        }\n    }\n    return x;\n};\n```
['JavaScript']