programming-examples/ruby/Data_Structures/missing_number.rb

13 lines
315 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
# Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
#
# @param {Integer[]} numbers
# @return {Integer}
def missing_number(numbers)
last = -1
numbers.each do |number|
return number - 1 unless number - 1 == last
last = number
end
end