CJCoding With Joseph
15per day

Find Second Largest

An array called nums has already been created for you with the values {10, 40, 20, 50, 30}.

Your task is to find and print the second largest value in the array.

To solve this, you need to track two values as you loop through the array:
- The largest value seen so far
- The second largest value seen so far

Start by setting both to very small values (or use the first two elements). As you check each element, decide whether it becomes the new largest (bumping the old largest down to second) or just the new second largest.

The largest value is 50, so the second largest is 40.

Print the result in this exact format:
Second largest: 40

Expected Output:

Second largest: 40
Topics:
Arrays
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.