Documentation
    Preparing search index...
    • Generates all possible subsets of a given array.

      Implements the mathematical concept of power set, generating 2^n subsets from an array of n elements. Uses depth-first search (DFS) algorithm to calculate all possible combinations of including or excluding each element.

      Type Parameters

      • T

        The type of elements in the array

      Parameters

      • array: T[]

        The array to generate subsets from

      Returns T[][]

      An array containing all possible subsets

        const numbers = [1, 2, 3];
      const allSubsets = ArrayUtil.subsets(numbers);
      console.log(allSubsets);
      // [
      // [], // empty set
      // [3], // {3}
      // [2], // {2}
      // [2, 3], // {2, 3}
      // [1], // {1}
      // [1, 3], // {1, 3}
      // [1, 2], // {1, 2}
      // [1, 2, 3] // {1, 2, 3}
      // ]

      const colors = ['red', 'blue'];
      const colorSubsets = ArrayUtil.subsets(colors);
      console.log(colorSubsets);
      // [
      // [],
      // ['blue'],
      // ['red'],
      // ['red', 'blue']
      // ]

      // Warning: Result size grows exponentially with array size
      // Example: 10 elements → 1,024 subsets, 20 elements → 1,048,576 subsets