Monday, 26 August 2019

How to find the missing elements in an array using ruby?

Input: a= [3, 4, 5, 6, 7, 15]

min_element  = a.min
max_element = a.max
new_missing_elements = []

for i in (min_element..max_element)
   new_missing_elements.push(i) unless a.include?(i)
end

print new_missing_elements

OUTPUT:

=>[8, 9, 10, 11, 12, 13, 14]

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...