Find minimum array element in Ruby
Last Updated :
24 Oct, 2019
Improve
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:
ruby
Output:
ruby
Output:
ruby
Output:
# 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
# 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
1 1 G4GExample #2:
# Function to find the min using max method
def min(*arr)
arr.min
end
print min(1, 2, 3, 4, 5)
# Function to find the min using max method
def min(*arr)
arr.min
end
print min(1, 2, 3, 4, 5)
1Example #3: A bit slower method
# Using enumerable#max
val = ('1'..'6').to_a.min
print val
# Using enumerable#max
val = ('1'..'6').to_a.min
print val
1