Open In App

Find minimum array element in Ruby

Last Updated : 24 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads