Function indexOfMax [src]

Returns the index of the largest number in a slice. O(n). slice must not be empty.

Prototype

pub fn indexOfMax(comptime T: type, slice: []const T) usize

Parameters

T: typeslice: []const T

Example

test indexOfMax { try testing.expectEqual(indexOfMax(u8, "abcdefg"), 6); try testing.expectEqual(indexOfMax(u8, "gabcdef"), 0); try testing.expectEqual(indexOfMax(u8, "a"), 0); }

Source

pub fn indexOfMax(comptime T: type, slice: []const T) usize { assert(slice.len > 0); var best = slice[0]; var index: usize = 0; for (slice[1..], 0..) |item, i| { if (item > best) { best = item; index = i + 1; } } return index; }