How do I find the first two consecutive elements in my array of numbers?

前端 未结 5 789
长发绾君心
长发绾君心 2021-01-25 06:06

Using Ruby 2.4, I have an array of unique, ordered numbers, for example

[1, 7, 8, 12, 14, 15]

How do I find the first two elements whose differ

5条回答
  •  面向向阳花
    2021-01-25 06:55

    Simple example

       X = [1, 7, 8, 12, 14, 15]
    
       X.each_with_index do |item, index|
        if index < X.count - 1
         if (X[index+1]-X[index] == 1) 
          puts item
         end
       end
      end
    

提交回复
热议问题