Write a program to find the second largest element in the array
class Array
{
public static void main(String args[])
{
int arr[]=new int[5]; //50 10 20 60 55
0 1 2 3 4
int i;
Scanner s=new Scanner(System.in);
for(i=0;i<arr.length;i++)
{
arr[i]=s.nextInt();// to fill the values in an array
}
int fmax=arr[0]; //50
int smax=arr[0]; //50
for(i=1;i<arr.length;i++)
{
if(fmax < arr[i]) //50<10(f) 50<20(f) 50<60(t) 60<55(f)
{
smax=fmax;———————————>smax=50
fmax=arr[i];——————————->fmax=60
}
else if(smax < arr[i]) //50<10(f) 50<20(f)————–50<55(t)
{
smax = arr[i];——————————————>smax=55
}
}
System.out.println(“Second Largest=” +smax);
}
}