Find Second Largest in Array

· unclassifiedUnclassifiedarrays

Find Second Largest in Array

javamay contain transcription errors
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for(int num : arr){
if(num > first){
second = first;
first = num;
} else if(num > second && num != first){
second = num;
}
}
System.out.println(second);
}
}
← Find Missing Number in 1 to N+1GCD and LCM →
Report an issue with this question