Skip to content

Instantly share code, notes, and snippets.

@tandavala
Created May 11, 2020 14:28
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 tandavala/220e398c0eeab920ad7ea04e321b3b46 to your computer and use it in GitHub Desktop.
Save tandavala/220e398c0eeab920ad7ea04e321b3b46 to your computer and use it in GitHub Desktop.
function print3largest(array, size){
let i, first, second, third;
if(size < 3){
throw new Error("Invalid Input");
return;
}
third = first = second = Number.MIN_VALUE;
for(i =0; i < size; i++){
// If current element is greater than frist
if(array[i] > first){
third = second;
second = first;
first = array[i];
}
// if array[i] is in between first and second then update second
else if(array[i] > second){
third = second;
second = array[i];
}
else if(array[i] > third){
third = array[i];
}
}
console.log(` ${first}, ${second}, ${third}`);
}
let array = [12, 2, 43, 1000, 7, 55, 65, 100];
console.log(array)
print3largest(array, 8);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment