Skip to content

Instantly share code, notes, and snippets.

@YonatanKra
Last active September 2, 2021 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YonatanKra/523dd8421aa1d7d8a72da41588da9c21 to your computer and use it in GitHub Desktop.
Save YonatanKra/523dd8421aa1d7d8a72da41588da9c21 to your computer and use it in GitHub Desktop.
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