Last active
September 2, 2021 20:33
-
-
Save YonatanKra/523dd8421aa1d7d8a72da41588da9c21 to your computer and use it in GitHub Desktop.
This file contains 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
function getNestedArraysMaxLength(inputArray) { | |
let maxLength = -Infinity; | |
inputArray.forEach((value, index) => { | |
const seriesLength = findSeriesLength(inputArray, index); | |
maxLength = Math.max(maxLength, seriesLength); | |
}); | |
return maxLength; | |
} | |
function findSeriesLength(arr, nextIndex) { | |
let count = 0; | |
while (true) { | |
let nextValue = arr[nextIndex]; | |
if (nextValue === null) { | |
return count; | |
} | |
arr[nextIndex] = null; | |
nextIndex = nextValue; | |
count++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment