1var subsetsWithDup = function (nums) {2let result = [];3//sort the nums to avoid duplicates;4nums.sort((a, b) => a - b);5result.push([]);67let startIdx = 0;8let endIdx = 0;910for (let i = 0; i < nums.length; i++) {11let current = nums[i];12startIdx = 0;1314//check for duplicates and get the idx of last subset15if (i > 0 && nums[i] === nums[i - 1]) {16startIdx = endIdx + 1;17}18endIdx = result.length - 1;1920for (let j = startIdx; j < endIdx + 1; j++) {21let set1 = result[j].slice(0);22set1.push(current);23result.push(set1);24}25}2627return result;28};