Subsets of an array | JavaScript

CodeCatch

April 25th, 2021 09:54:59 PM

					
const getSubsets = arr => arr.reduce((prev, curr) => prev.concat(prev.map(k => k.concat(curr))), [[]]); // Examples getSubsets([1, 2]); // [[], [1], [2], [1, 2]] getSubsets([1, 2, 3]); // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]

Featured Posts