Function kCombinations

  • K-combinations

    Examples:

    kCombinations([1, 2, 3], 1)  // [[1], [2], [3]]
    kCombinations([1, 2, 3], 2) // [[1, 2], [1, 3], [2, 3]]
    kCombinations([1, 2, 3], 3) // [[1, 2, 3]]
    kCombinations([1, 2, 3], 4) // []
    kCombinations([1, 2, 3], 0) // []
    kCombinations([1, 2, 3], -1) // []
    kCombinations([], 0) // []

    Type Parameters

    • T

    Parameters

    • set: T[]

      Array of objects of any type. They are treated as unique.

    • k: number

      Size of combinations to search for.

    Returns T[][]

    Array of found non-empty combinations, size of a combination is k.