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)
    maxElement
    (
    alias map = (a => a)
    Range
    )
    (
    Range r
    )
    if (
    isInputRange!Range &&
    !isInfinite!Range
    )
  2. auto maxElement(Range r, RangeElementType seed)

Parameters

map

custom accessor for the comparison key

r Range

range from which the maximum element will be selected

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