Created
August 20, 2024 05:27
-
-
Save vishnuroshan/4bf1c17849e288836da0af0456fb19e2 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
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is a tiger analytics interview question.