Skip to content

Instantly share code, notes, and snippets.

@williamromero
Last active May 5, 2022 18:39
Show Gist options
  • Save williamromero/e480d8c55ed731ddbf45d8421d08ff63 to your computer and use it in GitHub Desktop.
Save williamromero/e480d8c55ed731ddbf45d8421d08ff63 to your computer and use it in GitHub Desktop.
This code checks if exists a sequence of numbers inside of an array and retrieves those numbers ordered.
def sequence_length(array)
array.sort { |a, b| a <=> b }.each_cons(2) { |a, b| b - a == 1 ? array : array.delete(b) }
"Length on sequence: #{array.length} - #{array.sort}"
end
p sequence_length([100, 4, 200, 1, 3, 2]) # Length on sequence: 4 - [1, 2, 3, 4]
p sequence_length([29, 27, 28, 55, 100, 84]) # Length on sequence: 3 - [27, 28, 29]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment