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

前端 未结 5 770
长发绾君心
长发绾君心 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:51

    Here's an alternate method provided for educational purposes:

    arr = [1, 7, 8, 12, 14, 15]
    
    arr.each_cons(2).map {|v|v.reduce(:-)}.index(-1)
    

提交回复
热议问题