Find second largest element | Practice | GeeksforGeeks

Given an array of elements. Your task is to find the second maximum element in the array.
Example 1:
Input:
N=5
arr[] = { 2, 4, 5, 6, 7 }
Output:  6
Explanation:
The largest element is 7 and 
the second largest element is 6.

 
Example 2:
Input:
N=6
arr[] = { 7, 8, 2, 1, 4, 3 }
Output:  7


Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function print2largest() that takes array A and integer N as parameters and returns the second maximum element in the array. If there does not exist any second largest element, then return -1.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 106
 
0
emaniharsha8 hours ago
Working O(n) solution in single loop:

print2largest(arr, n){
    let [first, second] = [arr[0], -1];

    for(let i = 0; i < n; i++){
        if(first < arr[i]){
            second = first;
            first = arr[i];
        } else if(arr[i] > second && arr[i] != first)
            second = arr[i];
    }

    return second;
}
0
pradeepshillare3 days ago
int ans = -1;      Arrays.sort(A);      for(int i=0;i<A.length-1;i++)      {          if(A[i]!=A[i+1])          {              ans = A[i];          }      }      return ans;
0
pradeepshillare3 days ago
Set<Integer> s = new HashSet<Integer>();      for(int i=0;i<N;i++)      {          s.add(A[i]);      }      List<Integer> l = new ArrayList<Integer>(s);      if(s.size()==1)           return -1;      else          Collections.sort(l);          int u = l.get(s.size()-2);          return u;
0
kushganesh124 days ago
blic static int print2largest(int A[],int N)    {       Arrays.sort(A);       int mp=-1;       for(int i=0;i<N-1;i++)       {           if(A[i]<A[i+1])            mp=A[i];                  }
       return mp;   }
0
vishalja77195 days ago
 int print2largest(int arr[], int arr_size)
    {
    	//code here.
     set<int>s;
     for(int i=0;i<arr_size;i++)
     {
         s.insert(arr[i]);
     }
     if(s.size() == 1)
        return -1;
      else{
          auto it=s.end();
          --it;
          --it;
          
          return *it;
      }
    }
-2
nishabisht2236 days ago
public static int print2largest(int A[],int N)    {       Arrays.sort(A);       return A[A.length-2];   }
+1
cs190951 week ago
//simple c++ solution using vector and set
int print2largest(int arr[], int n)
    {
    	//code here.
    	int ans;
    	vector<int>v;
    	set<int>s;
    	for(int i=0;i<n;i++)
    	{
    	    s.insert(arr[i]);
    	}
    	if(s.size()==1)
    	{
    	    return -1;
    	}
    	else
    	{
    	
    	v.assign(s.begin(),s.end());
    	int size=s.size();
    	sort(v.begin(),v.end());
    	 ans=v.at(size-2);
    	 return ans;
    }
    }
0
harshilrpanchal19981 week ago
java
 
int first=0,second=0;      for(int i=0;i<N;i++)      {          if(A[i]>first)          first=A[i];      }       for(int i=0;i<N ;i++)      {          if(A[i]>second && A[i]<first)          second=A[i];      }            if(second==0 || N<2){      return -1;}            return second;
0
harshilrpanchal19981 week ago
java solution
 
HashSet<Integer> set = new HashSet<>();       for (int element: A){           set.add(element);       }
       int[] array = new int[set.size()];       int i=0;       for (Integer ele: set){           array[i++] = ele;       }       Arrays.sort(array);
       if (N == 1){           return array[0];       }       if(N == 0){           return -1;       }       return array[array.length -2];
+1
harshscode1 week ago
O(n).............................................................
 
int first=0,second=0;       for(int i=0;i<n;i++)       {           if(a[i]>first)           first=a[i];       }        for(int i=0;i<n;i++)       {           if(a[i]>second and a[i]<first)           second=a[i];       }              if(second==0 or n<2)       return -1;       else        return second;          }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.