Skip to content

Instantly share code, notes, and snippets.

@lancejpollard
Last active June 6, 2021 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancejpollard/817bb054011ca59b54c1444bdc41fb75 to your computer and use it in GitHub Desktop.
Save lancejpollard/817bb054011ca59b54c1444bdc41fb75 to your computer and use it in GitHub Desktop.
Compute the Powerset of a Set in JavaScript
const computePowerSetOfSetOfNumbers = (set) => {
const powerset = []
const n = set.length
let i = 0
while (i < n) {
let x = n
while (x > i) {
powerset.push(set.slice(i, x))
x--
}
i++
}
return powerset
}
const set = [1, 2, 3, 4, 5]
const powerset = computePowerSetOfSetOfNumbers(set)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment