Implement a function that returns all subsets of a given array.
HardProblem Statement
Implement a function that returns all subsets of a given array.
Sample Input
[ 1, 2, 3 ]
Sample Output
[ [], [ 1 ], [ 2 ], [ 3 ], [ 1, 2 ], [ 1, 3 ], [ 2, 3 ], [ 1, 2, 3 ] ]
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
// Generate all subsets of an array
function getSubsets(arr) {
// Initialize an array with an empty subset
// Loop through elements, generating new subsets by adding each element to existing subsets
}
console.log(getSubsets([ 1, 2, 3 ]));
Input
[ 1, 2, 3 ]
Expected Output
[ [], [ 1 ], [ 2 ], [ 3 ], [ 1, 2 ], [ 1, 3 ], [ 2, 3 ], [ 1, 2, 3 ] ]