SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method Subsets

    | Edit this page

    Subsets<T>(IEnumerable<T>)

    Returns a sequence of IList<T> representing all of the subsets of any size that are part of the original sequence. In mathematics, it is equivalent to the power set of a set.

    Declaration
    public static IEnumerable<IList<T>> Subsets<T>(this IEnumerable<T> sequence)
    Parameters
    Type Name Description
    IEnumerable<T> sequence

    Sequence for which to produce subsets

    Returns
    Type Description
    IEnumerable<IList<T>>

    A sequence of lists that represent the all subsets of the original sequence

    Type Parameters
    Name Description
    T

    The type of the elements in the sequence

    Remarks

    This operator produces all of the subsets of a given sequence. Subsets are returned in increasing cardinality, starting with the empty set and terminating with the entire original sequence. Subsets are produced in a deferred, streaming manner; however, each subset is returned as a materialized list. There are 2^N subsets of a given sequence, where N = sequence.Count().

    This method is implemented by using deferred execution. However, sequence will be consumed in it's entirety immediately when first element of the returned sequence is consumed.

    Examples

    The following code example demonstrates how to get all subsets of a sequence using Subsets.

    var sequence = Enumerable.Range(0, 4);
    
    // check that sequence starts with a known sequence of values
    var result = sequence
        .Subsets();
    
    Console.WriteLine(
        "[" + Environment.NewLine + 
        string.Join(
            ", " + Environment.NewLine,
            result.Select(c => "   [" + string.Join(", ", c) + "]")) +
        Environment.NewLine + "]");
    
    // This code produces the following output:
    // [
    //    [],
    //    [0],
    //    [1],
    //    [2],
    //    [3],
    //    [0, 1],
    //    [0, 2],
    //    [0, 3],
    //    [1, 2],
    //    [1, 3],
    //    [2, 3],
    //    [0, 1, 2],
    //    [0, 1, 3],
    //    [0, 2, 3],
    //    [1, 2, 3],
    //    [0, 1, 2, 3]
    // ]
    
    Exceptions
    Type Condition
    ArgumentNullException

    sequence is null

    | Edit this page

    Subsets<T>(IEnumerable<T>, int)

    Returns a sequence of IList<T> representing all subsets of a given size that are part of the original sequence. In mathematics, it is equivalent to the combinations or k-subsets of a set.

    Declaration
    public static IEnumerable<IList<T>> Subsets<T>(this IEnumerable<T> sequence, int subsetSize)
    Parameters
    Type Name Description
    IEnumerable<T> sequence

    Sequence for which to produce subsets

    int subsetSize

    The size of the subsets to produce

    Returns
    Type Description
    IEnumerable<IList<T>>

    A sequence of lists that represents of K-sized subsets of the original sequence

    Type Parameters
    Name Description
    T

    The type of the elements in the sequence

    Examples

    The following code example demonstrates how get all subsets of a particular length using Subsets.

    var sequence = Enumerable.Range(0, 4);
    
    // check that sequence starts with a known sequence of values
    var result = sequence
        .Subsets(2);
    
    Console.WriteLine(
        "[" + Environment.NewLine + 
        string.Join(
            ", " + Environment.NewLine,
            result.Select(c => "   [" + string.Join(", ", c) + "]")) +
        Environment.NewLine + "]");
    
    // This code produces the following output:
    // [
    //    [0, 1],
    //    [0, 2],
    //    [0, 3],
    //    [1, 2],
    //    [1, 3],
    //    [2, 3]
    // ]
    
    Exceptions
    Type Condition
    ArgumentNullException

    sequence is null

    ArgumentOutOfRangeException

    subsetSize is less than 0.

    © SuperLinq Authors. All rights reserved.