Skip to content
Related Articles

Related Articles

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

1
1
G4G

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Function to find the min using max method
def min(*arr)
 arr.min
end
  
print min(1, 2, 3, 4, 5)

chevron_right


Output:

1

Example #3: A bit slower method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Using enumerable#max
val = ('1'..'6').to_a.min
print val

chevron_right


Output:

1



My Personal Notes arrow_drop_up
Recommended Articles
Page :