# Is there any way to use max() and min() based on a function?

**URL:** <https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350>\
**Category:** General Usage\
**Tags:** syntax\
**Created:** [December 16, 2019, 7:38pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350 "2019-12-16T19:38:19Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 20, 2022, 2:28pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350/10 "2022-01-20T14:28:28Z")

</div>

You can also pass the function to argmax, to avoid the allocation of the intermediate array:

```julia
julia> x = [1,3,2]
3-element Vector{Int64}:
 1
 3
 2

julia> argmax(x -> -x^2, x)
1

julia> argmax(x -> x^2, x)
3

```

---

_[View the full topic](https://discourse.julialang.org/t/is-there-any-way-to-use-max-and-min-based-on-a-function/32350)._
