SOURCE

const arrTest = [1, 2, 3];

const subsets = (arr) => {
    const result = [[]];
    const len = arr.length;

    const helper = (target) => {
        const len2 = result.length;

        for (let i = 0; i < len2; i++) {
            result.push([...result[i], target]);
        }
    }

    for (let i = 0; i < len; i++) {
        helper(arr[i])
    }

    return result;
};
// console.log(subsets(arrTest));

const arrTest2 = [1, 2, 2, 3];

const subsetsWithDup = (arr) => {
    const len = arr.length;
    const result = [[]];
    const obj = {};

    for (let i = 0; i < len; i++) {
        if (!obj[arr[i]]) {
            for (let j = i; j < len; j++) {
                result.push(arr.slice(i, j + 1));
            }
        }
        obj[arr[i]] = arr[i];
    }

    return result;
};
console.log(subsetsWithDup(arrTest2));
console 命令行工具 X clear

                    
>
console