Back to Lo

It Maxindex

docs/data/it-maxindex.md

1.53.0645 B
Original Source

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:

go
// 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