Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thmain/19a15472878ebf0c5ac798ed52f6ba80 to your computer and use it in GitHub Desktop.
Save thmain/19a15472878ebf0c5ac798ed52f6ba80 to your computer and use it in GitHub Desktop.
const lengthofLargestSubarrayWithContinuousElements = (arr) => {
arr.sort()
let count = []
let inLen = arr.length
let j = 0
let initalCount = 1
for (let i = 0; i < inLen; i++) {
while( (arr[i] + 1) == arr[i+1] ) {
count[j] = initalCount + 1
initalCount++
i++
}
initalCount = 1
j++
}
// filter null elements from an array
count = count.filter(Number)
return Math.max(...count)
};
console.log(lengthofLargestSubarrayWithContinuousElements([4, 2, 1, 20])) // 2
console.log(lengthofLargestSubarrayWithContinuousElements([2, 4, 3])) // 3
console.log(lengthofLargestSubarrayWithContinuousElements([1, 563, 585, 571, 90, 92, 94, 93, 91, 45])) // 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment