10. Find second largest element
Given an array of elements. Your task is to find the second maximum element in the array. If there does not exist any second largest element, then return -1.
Array contains duplicates
Solution: (Two Traversal)
Approach: Find the maximum element Find the second maximum which is not equal to maximum
Time Complexity: O(n)
Solution: (Using one traversal)
Use two variables max and second max If value is greater than max: second max = max max = value If value lies in between: second max = value
Time Complexity: O(n)
Last updated