SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method RunLengthEncode

    | Edit this page

    RunLengthEncode<T>(IEnumerable<T>)

    Run-length encodes a sequence by converting consecutive instances of the same element into a tuple representing the item and its occurrence count.

    Declaration
    public static IEnumerable<(T value, int count)> RunLengthEncode<T>(this IEnumerable<T> sequence)
    Parameters
    Type Name Description
    IEnumerable<T> sequence

    The sequence to run length encode

    Returns
    Type Description
    IEnumerable<(T value, int count)>

    A sequence of tuples containing the element and the occurrence count

    Type Parameters
    Name Description
    T

    The type of the elements in the sequence

    Remarks

    This operator evaluates in a deferred and streaming manner.

    Examples

    The following code example demonstrates how to return the run-length-encoding of a sequence using RunLengthEncode.

    var sequence = Enumerable.Repeat(1, 3)
        .Concat(Enumerable.Repeat(2, 4))
        .Concat(Enumerable.Repeat(3, 2));
    
    // Get the run-length encoding of a sequence
    var result = sequence.RunLengthEncode();
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [(1, 3), (2, 4), (3, 2)]
    
    Exceptions
    Type Condition
    ArgumentNullException

    sequence is null.

    | Edit this page

    RunLengthEncode<T>(IEnumerable<T>, IEqualityComparer<T>?)

    Run-length encodes a sequence by converting consecutive instances of the same element into a tuple representing the item and its occurrence count. This overload uses a custom equality comparer to identify equivalent items.

    Declaration
    public static IEnumerable<(T value, int count)> RunLengthEncode<T>(this IEnumerable<T> sequence, IEqualityComparer<T>? comparer)
    Parameters
    Type Name Description
    IEnumerable<T> sequence

    The sequence to run length encode

    IEqualityComparer<T> comparer

    The comparer used to identify equivalent items

    Returns
    Type Description
    IEnumerable<(T value, int count)>

    A sequence of tuples containing the element and the occurrence count

    Type Parameters
    Name Description
    T

    The type of the elements in the sequence

    Remarks

    This operator evaluates in a deferred and streaming manner.

    Examples

    The following code example demonstrates how to return the run-length-encoding of a sequence using RunLengthEncode.

    var sequence = new[]
    {
        "foo",
        "FOO",
        "fOo",
        "bAr",
        "BAR",
        "BAZ",
        "baz",
        "Baz",
        "BaZ",
        "Qux",
    };
    
    // Get the run-length encoding of a sequence
    var result = sequence
        .RunLengthEncode(
            StringComparer.OrdinalIgnoreCase);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [(foo, 3), (bAr, 2), (BAZ, 4), (Qux, 1)]
    
    Exceptions
    Type Condition
    ArgumentNullException

    sequence is null.

    © SuperLinq Authors. All rights reserved.