I have an array of elements. If I do a arr.max
I will get the maximum value. But I would like to get the index of the array. How to find it in Ruby
For example
a = [3,6,774,24,56,2,64,56,34] => [3, 6, 774, 24, 56, 2, 64, 56, 34] >> a.max a.max => 774
I need to know the index of that 774
which is 2
. How do I do this in Ruby?
To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.
a.index(a.max) should give you want you want
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With