This post is completed by 1 user
|
Add to List |
153. Find the Second Largest Element in an Array
Objective: Given an array of integers. find the second largest element in the array.
Example:
int[] A = { 1, 2, 10, 20, 40, 32, 44, 51, 6 }; Second largest Element : 44
Approach:
- Keep track of the largest element and whenever you change the value of the largest element, store its current value to another variable, and call it the second largest element.
- If you are not updating the largest element then check if the second largest element is less than the current element, if yes then update it.
Output:
Second largest Element : 44