Selecting The Array Index With The Maximum Value

by Kyle on · Posted in Ruby

To find the index for the maximum value in an array, two array methods are chained together, Enumerable#each_with_index and Enumerable#max.

a = [3,4,5,2]
 
a.each_with_index.max
#=> [5, 2]
 
a.each_with_index.min
#=> [2, 3]

Save. Share. Submit.

Rate Snippet

Related Snips

Hide Sidebar