Return the largest number in the nums array. The array has at least one number.
largest(new int[] {-4, -9, -2}) returns -2.
10 strips: U1 to U10
max = nums[i]; U1return max; U2int max = 0; U3public static int largest(int[] nums) { U4if (nums[i] > max) { U5for (int i = 1; i < nums.length; i++) { U6int max = nums[0]; U7} U8} U9} U10Order: U4, U7, U6, U5, U1, U9, U8, U2, U10. Extra: U3
public static int largest(int[] nums) {
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
return max;
} Extra strip: int max = 0;. Starting at 0 only works when some number is above 0. For {-4, -9, -2} it returns 0, which is not in the array.