Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the index of an array which has a maximum value

Tags:

arrays

ruby

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?

like image 405
bragboy Avatar asked Aug 21 '10 19:08

bragboy


People also ask

How do you find the index value of an array?

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.


1 Answers

a.index(a.max)  should give you want you want 
like image 111
ennuikiller Avatar answered Oct 04 '22 12:10

ennuikiller