← Back to challenges

Almost Uniform Sequence

JavaScriptHardarraysconditionsloops

Instructions

Find the length of the longest sub-sequence of two distinct numbers whose difference is 1. A sub-sequence can be made by deleting any numbers in between.

Examples

almostUniform([1, 3, 2, 2, 5, 2, 3, 7]) ➞ 5
// [3, 2, 2, 2, 3]

almostUniform([1, 2, 3, 4]) ➞ 2
// [1, 2] or [2, 3] or [3, 4]

almostUniform([1, 1, 1, 1]) ➞ 0
// There is no sub-sequence of two distinct numbers.

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.