Here's a function in TypeScript that takes a set as input and returns an array of all its subsets:
index.ts555 chars28 lines
The function works by first converting the input set into an array, then using a loop to calculate all possible subsets (2^n where n is the size of the input set). Each subset is represented by a binary number where a value of 1 indicates that the corresponding element is included and a value of 0 indicates that it is not.
The loop converts each subset index to binary and adds the elements corresponding to the set bits to the current subset. The resulting subset is then added to a final array of subsets that is returned at the end.
gistlibby LogSnag