Files
lo/docs/data/it-maxindex.md
T
Samuel Berthe fa095e4b4f fix(doc): fix go playground demo URL (#832)
* fix(doc): fix go playground demo URL

* fix(doc): add more go playground demo URL
2026-03-06 00:09:59 +01: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/zeu2wUvhl5e
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