Skip to content

Instantly share code, notes, and snippets.

@ramsunvtech
Created September 29, 2018 23:36
Show Gist options
  • Save ramsunvtech/f1ff4b098cb3be1fe457af95081fd4f6 to your computer and use it in GitHub Desktop.
Save ramsunvtech/f1ff4b098cb3be1fe457af95081fd4f6 to your computer and use it in GitHub Desktop.
Largest Number of Beads Problem
function solution(A) {
let longest = [];
let uniqueList = [];
let lengthList = [];
if (!Array.isArray(A) || A.length === 0) {
return 0;
}
for (let i = 0; i < A.length; i++) {
let items = [];
function getItems(j) {
if(A[j] >= 0 && items.indexOf(A[j]) === -1 && uniqueList.indexOf(A[j]) === -1) {
items.push(A[j]);
uniqueList.push(A[j]);
return getItems(A[j]);
}
return items;
}
const totalItems = getItems(i);
if (totalItems.length > 0) {
lengthList.push(totalItems.length);
longest[i] = totalItems;
}
};
return Math.max(...lengthList);
}
solution([5, 4, 0, 3, 1, 6, 2]);
// 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment