gpt4 book ai didi

ruby-on-rails - 如何找到数组中最大值的位置?

转载 作者:数据小太阳 更新时间:2023-10-29 07:48:50 25 4
gpt4 key购买 nike

如果我有

ary = [7, 8, 0, 1, nil, 6]

如何找到数组中最大值的位置?我可以做到这一点,但它需要不止一行。

最佳答案

这将返回数组中第一个最大值的索引:

ary = [7, 8, 0, 1, nil, 6, 8]
ary.index(ary.compact.max)
=> 1

关于ruby-on-rails - 如何找到数组中最大值的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6244600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com