maxElement

Iterates the passed range and returns the maximal element. A custom mapping function can be passed to map. In other languages this is sometimes called argmax.

Complexity: O(n) Exactly n - 1 comparisons are needed.

  1. auto maxElement(Range r)
  2. auto maxElement(Range r, RangeElementType seed)
    maxElement
    (
    alias map = (a => a)
    Range
    RangeElementType = ElementType!Range
    )
    (
    Range r
    ,
    RangeElementType seed
    )
    if (
    isInputRange!Range &&
    !isInfinite!Range
    &&
    !is(CommonType!(ElementType!Range, RangeElementType) == void)
    )

Parameters

map

custom accessor for the comparison key

r Range

range from which the maximum element will be selected

seed RangeElementType

custom seed to use as initial element

Precondition: If a seed is not given, r must not be empty.

Return Value

Type: auto

The maximal element of the passed-in range.

Note: If at least one of the arguments is NaN, the result is an unspecified value. See std.algorithm.searching.minElement for examples on how to cope with NaNs.

Examples

import std.range : enumerate;
import std.typecons : tuple;
assert([2, 1, 4, 3].maxElement == 4);

// allows to get the index of an element too
assert([2, 1, 4, 3].enumerate.maxElement!"a.value" == tuple(2, 4));

// any custom accessor can be passed
assert([[0, 4], [1, 2]].maxElement!"a[1]" == [0, 4]);

// can be seeded
int[] arr;
assert(arr.minElement(1) == 1);

See Also

Meta