I have tried the following method to find second largest & smallest number and it works :
class Test
{
static void main()throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter");
int a[] = {45,79,2,5,74,4,19,56,2,888};
int small= a[0];
int big=a[0];
int s=0,b=0;
for(int i=0;i<10;i++)
{
if(small>a[i])
{
small=a[i];
}
if(big<a[i])
{
big=a[i];
}
}
int small2=a[0],big2=a[0];
for(int i=0;i<10;i++)
{
if(small2>a[i]&&a[i]!=small)
{
small2=a[i];
}
if(big2<a[i]&&a[i]!=big)
{
big2=a[i];
}
}
System.out.println("second biggest = "+big2);
System.out.println("second smallest = "+small2);
}
}
Now I want to find the fourth greatest and lowest. in this case i need to use 4 loops probably. but i want to do it in a shorter, smarter way. Can somebody help ?