// M2 Looping intmin1= Integer.MAX_VALUE; // the smallest intmin2= Integer.MAX_VALUE; // the second smallest
intmax1= Integer.MIN_VALUE; // the largest intmax2= Integer.MIN_VALUE; // the second largest intmax3= Integer.MIN_VALUE; // the third largest
for (int n:nums) { if (n<=min1) { // n become the new smallest min2 = min1; min1 = n; } elseif (n<=min2) { // n become the new second smallest min2 = n; }
if (n>=max1) { // n become the new largest max3 = max2; max2 = max1; max1 = n; } elseif (n>=max2) { // n become the new second largest max3 = max2; max2 = n; } elseif (n>=max3) { // n become the new third largest max3 = n; } }