Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JellyZhang opened this issue Dec 16, 2021 · 0 comments

Comments

@JellyZhang
Copy link

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.

@thoas thoas changed the title [feature request] how about adding argMax which return the index of max element from a slice ? how about adding argMax which return the index of max element from a slice ? Dec 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants