Skip to content

Note: 순열 & 조합 #4

@YimJiYoung

Description

@YimJiYoung
function getAllSubsets(array) {
    const subsets = [[]];
    
    for (const el of array) {
        const last = subsets.length-1;
        for (let i = 0; i <= last; i++) { // 기존 subset 배열에 자신을 추가한 새로운 배열 만들어 push
            subsets.push( [...subsets[i], el] ); 
        }
    }
    
    return subsets;
}

Here is an example for [1, 2, 3]

  • Start with an empty subset: []

  • Create new subsets by adding "1" to each existing subset. It will be:[] [1]

  • Create new subsets by adding "2" to each existing subset. It will be:[], [1] [2], [1, 2]

  • Create new subsets by adding "3" to each existing subset. It will be: [], [1], [2], [1, 2] [3], [1, 3], [2, 3], [1, 2, 3]

참고 : https://stackoverflow.com/questions/42773836/how-to-find-all-subsets-of-a-set-in-javascript

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions