sliding window, hash and sort on js and ts.
Code\njavascript []\nfunction findXSum(nums, k, x) {\n    let l = 0\n    const res = []\n    const freq = {}\n    for (let r = 0; r < nums.length; r++) {\n     
# Code\n```javascript []\nfunction findXSum(nums, k, x) {\n    let l = 0\n    const res = []\n    const freq = {}\n    for (let r = 0; r < nums.length; r++) {\n        // if out of window update left and freq\n        if (r - l + 1 > k) {\n            freq[nums[l]]--\n            l++\n        }\n\n        // update right and freq\n        freq[nums[r]] = (freq[nums[r]] || 0) + 1\n\n        // if stay at window\n        if (r - l + 1 === k) {\n            // convert freq to arr\n            const arr = []\n            for (const key in freq) arr.push([key, freq[key]])\n\n            // then sort if same value use element compare else use value\n            // [element, frequency]\n            arr.sort((a, b) => a[1] === b[1] ? b[0] - a[0] : b[1] - a[1])\n\n            // final sum by x\n            let sum = 0\n            for (let i = 0; i < x; i++) {\n                if (arr[i]) sum += (arr[i][0] * arr[i][1])\n            }\n            \n            // push result\n            res.push(sum)\n        }\n    }\n    return res\n};\n```\n```typescript []\nfunction findXSum(nums: number[], k: number, x: number): number[] {\n    let l = 0\n    const res = []\n    const freq = {}\n    for (let r = 0; r < nums.length; r++) {\n        // if out of window update left and freq\n        if (r - l + 1 > k) {\n            freq[nums[l]]--\n            l++\n        }\n\n        // update right and freq\n        freq[nums[r]] = (freq[nums[r]] || 0) + 1\n\n        // if stay at window\n        if (r - l + 1 === k) {\n            // convert freq to arr\n            const arr = []\n            for (const key in freq) arr.push([key, freq[key]])\n\n            // then sort if same value use element compare else use value\n            // [element, frequency]\n            arr.sort((a, b) => a[1] === b[1] ? b[0] - a[0] : b[1] - a[1])\n\n            // final sum by x\n            let sum = 0\n            for (let i = 0; i < x; i++) {\n                if (arr[i]) sum += (arr[i][0] * arr[i][1])\n            }\n            \n            // push result\n            res.push(sum)\n        }\n    }\n    return res\n};\n```
['Array', 'Hash Table', 'Sliding Window', 'TypeScript', 'JavaScript']