rubyist rubyist - 7 months ago 35
Ruby Question

Find second largest number from an array in Ruby

I have an array

a = [3,6,774,24,56,2,64,56,34]
. I need to find the second largest number in a single iteration using Ruby. How do I achieve it?

Answer

Simple:

array.sort[-2]

And you're done :)