Skip to content

Instantly share code, notes, and snippets.

@vishnuroshan
Created August 20, 2024 05:27
Show Gist options
  • Save vishnuroshan/4bf1c17849e288836da0af0456fb19e2 to your computer and use it in GitHub Desktop.
Save vishnuroshan/4bf1c17849e288836da0af0456fb19e2 to your computer and use it in GitHub Desktop.
// Definition of Leader: Any Element which is greater than all the values to its right
function findLeadersInArray(arr) {
const leaders = [];
arr.forEach((a, i)=> {
if (i=== arr.length-1) {
leaders.push(a);
return leaders;
}
const toCheck = arr.slice(i+1)
console.log(toCheck);
const isLeader = toCheck.every((e) =>{
if (a!==e) {
return a > e
} else return true
});
console.log(a, isLeader);
if (isLeader) leaders.push(a);
})
return leaders;
}
const ans = findLeadersInArray([12, 5, 8, 15, 14, 7, 2, 4])
console.log('ans:> ', ans);
@vishnuroshan
Copy link
Author

this is a tiger analytics interview question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment