← Back
Find the second largest element in an array.
Given an array of integers, find the second largest element.
Algorithm
- Initialize two variables, first and second, to -Infinity.
- Iterate through the array:
- If the current element is greater than first, update second to first and first to current element.
- If the current element is less than first but greater than second, update second to current element.
- Return or print the values.
Time Complexity
The time complexity of this algorithm is O(n), where n is the number of elements in the array.