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:
# min function on list arr =[ 1 , 2 , 3 , 4 , 5 ].min
print arr print "\n"
str1 = [ 1 , 2 , 3 , 4 , 5 ]
puts str1.min print "\n"
# min function on string str = [ "GFG" , "G4G" , "Sudo" , "Geeks" ]
print str.min |
Output:
1 1 G4G
Example #2:
# Function to find the min using max method def min(*arr)
arr.min
end print min( 1 , 2 , 3 , 4 , 5 )
|
Output:
1
Example #3: A bit slower method
# Using enumerable#max val = ( '1' .. '6' ).to_a.min
print val |
Output:
1
Article Tags :