如何找到数组中第二大元素的索引? [英] How can I find the index of the second greatest element in my array?

查看:38
本文介绍了如何找到数组中第二大元素的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby 2.4,并且有一个数字数组:

I'm using Ruby 2.4 and I have an array of numbers:

[23423, 349843, 13123, 29239, 20201, ...]

如何找到与数组中第二个最大值相对应的数组索引?您可以假设数组中至少有两个元素.

How can I find the array index corresponding to the second greatest value in the array? You can assume that there are at least two elements in the array.

推荐答案

尝试一下. a 是您的数组

a.index(a.max(2).last)

这篇关于如何找到数组中第二大元素的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆