Created
December 25, 2022 05:30
-
-
Save thmain/19a15472878ebf0c5ac798ed52f6ba80 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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