custom accessor for the comparison key
range from which the maximum element will be selected
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.
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);
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.