How to find all elements (leaders) in an array that are greater than all elements to their right in Node.js

1 Answer

0 votes
function PrintArrayLeaders(arr) {
    const size = arr.length;
    
    for (let i = 0; i < size; i++) {
        let j = 0;
        for (j = i + 1; j < size; j++) {
            if (arr[i] <= arr[j]) {
                break;
            }
        }
        if (j == size) {
            console.log(arr[i]);
        }
    }
}
        
const arr = [1, 99, 16, 5, 75, 9, 40, 50, 0, 19];


PrintArrayLeaders(arr);

  
  
  
  
/*
run:
  
99
75
50
19
  
*/

 



answered Aug 30, 2022 by avibootz
...