thoas / go-funk

A modern Go utility library which provides helpers (map, find, contains, filter, ...)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how about adding argMax which return the index of max element from a slice ?

JellyZhang opened this issue · comments

commented

Like the max function, argMax would return the index of max element from a slice. For example:

func argMax(nums []int64) int {
	if len(nums) == 0 {
		panic("error")
	}
	var maxNum int64
	var maxIndex int
	for i, num := range nums {
		if i == 0 {
			maxNum = num
			maxIndex = 0
			continue
		}
		if num > maxNum {
			maxNum = num
			maxIndex = i
		}
	}
	return maxIndex
}

If there are duplicate occurrences of max element, only return the first one.