I want to check if an arrays values once sorted are incrementing by 1
For example
[1, 2, 3, 4, 5] = TRUE
[1, 2, 8, 9, 10] = FALSE
Any suggestions are much appreciated
array = [1,2,4,3]
array.sort.each_cons(2).all? { |x,y| y == x + 1 }
This one doesn't require sort
.
a = [2, 8, 1, 9, 10]
b = a.dup
x = b.delete(b.min)
nil while b.delete(x+=1)
b.empty?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With