Find Two largest numbers in a list without using Array
If you exceed your maximum number (max1st
), your new maximum number will be set to num
. But your second largest number will be the current maximum number. So try this condition:
if (num > max1st) {
max2nd = max1st;
max1st = num;
} else if (num > max2nd) {
max2nd = num;
}