Files
lo/docs/data/it-maxindex.md
T
2025-10-08 19:35:53 +02:00

964 B

name, slug, sourceRef, category, subCategory, signatures, playUrl, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures playUrl variantHelpers similarHelpers position
MaxIndex maxindex it/find.go#L299 it find
func MaxIndex[T constraints.Ordered](collection iter.Seq[T]) (T, int)
https://go.dev/play/p/0HbUY4-zg
it#find#maxindex
core#slice#maxindex
480

Searches the maximum value of a collection and returns both the value and its index.

Returns (zero value, -1) when the collection is empty. Will iterate through the entire sequence.

Examples:

// Find the maximum value and its index
numbers := it.Slice([]int{5, 2, 8, 1, 9})
value, index := it.MaxIndex(numbers)
// value: 9, index: 4

// With empty collection
empty := it.Slice([]int{})
value, index := it.MaxIndex(empty)
// value: 0, index: -1

// Find the maximum string alphabetically and its index
words := it.Slice([]string{"apple", "zebra", "banana", "xylophone"})
value, index := it.MaxIndex(words)
// value: "zebra", index: 1