Monday, 29 April 2019

Find Maximum Element in an array

 Find the maximum elements in an array using ruby

def max_value(array)
  max = array[0]
  array.each do |i|
    if i > array[0]
      max = i
    end
  end
  max
end

array = [6,7,8,9,43,3,22,675,343,5454]
max_value(array) #calling method

No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...