Find minimum array element in Ruby
Last Updated :
24 Oct, 2019
In this article, we will learn how to find minimum array element in Ruby. There are multiple ways to find minimum array element in Ruby. Let’s understand each of them with the help of example.
Example #1:
arr =[ 1 , 2 , 3 , 4 , 5 ].min
print arr
print "\n"
str1 = [ 1 , 2 , 3 , 4 , 5 ]
puts str1.min
print "\n"
str = [ "GFG" , "G4G" , "Sudo" , "Geeks" ]
print str.min
|
Output:
1
1
G4G
Example #2:
def min(*arr)
arr.min
end
print min( 1 , 2 , 3 , 4 , 5 )
|
Output:
1
Example #3: A bit slower method
val = ( '1' .. '6' ).to_a.min
print val
|
Output:
1
Share your thoughts in the comments
Please Login to comment...