OSCC · JavaScript, Math, Algorithm · Sep 27, 2021

Returns the powerset of a given array of numbers.

const powerset = arr =>
  arr.reduce((a, v) => a.concat(a.map(r => r.concat(v))), [[]]);
powerset([1, 2]); // [[], [1], [2], [1, 2]]

Recommended snippets

  • Arithmetic progression

    JavaScript, Math

    Creates an array of numbers in the arithmetic progression, starting with the given positive integer and up to the specified limit.

  • Generates primes up to a given number, using the Sieve of Eratosthenes.

  • Greatest common divisor

    JavaScript, Math

    Calculates the greatest common divisor between two or more numbers/arrays.