Type Function indexOfMinMax [src]
Finds the indices of the smallest and largest number in a slice. O(n).
Returns the indices of the smallest and largest numbers in that order.
slice must not be empty.
Prototype
pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize }
Parameters
T: type
slice: []const T
Example
test indexOfMinMax {
try testing.expectEqual(.{ 0, 6 }, indexOfMinMax(u8, "abcdefg"));
try testing.expectEqual(.{ 1, 0 }, indexOfMinMax(u8, "gabcdef"));
try testing.expectEqual(.{ 0, 0 }, indexOfMinMax(u8, "a"));
}
Source
pub fn indexOfMinMax(comptime T: type, slice: []const T) struct { usize, usize } {
assert(slice.len > 0);
var minVal = slice[0];
var maxVal = slice[0];
var minIdx: usize = 0;
var maxIdx: usize = 0;
for (slice[1..], 0..) |item, i| {
if (item < minVal) {
minVal = item;
minIdx = i + 1;
}
if (item > maxVal) {
maxVal = item;
maxIdx = i + 1;
}
}
return .{ minIdx, maxIdx };
}