Learner Level
Implement the method findSecondLargest that returns the second largest distinct integer in an array.
Return the second largest distinct value from the supplied integer array. The array can contain duplicate values, negative values, and values in any order.
The task is designed to test careful handling of edge cases, not only the most common input.
- Use only the first
sizeelements. - The second largest value must be different from the largest value.
- The test data always contains at least two distinct values.
Example 1
Input
nums (int[]) =
[3, 1, 4, 2]size (int) =
4Returns (int)
3Example 2
Input
nums (int[]) =
[10, 10, 5, 3]size (int) =
4Returns (int)
5Example 3
Input
nums (int[]) =
[-1, -5, -3]size (int) =
3Returns (int)
-3Example 4
Input
nums (int[]) =
[7, 7, 6, 6, 5]size (int) =
5Returns (int)
6Track the largest and second largest distinct values while scanning the array. When a new largest value is found, move the old largest into the second largest slot.
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run