AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



programming-array: find second largest number in an array in efficient way in java

find second largest number in an array in efficient way in java

programming x 169
array x 12
Posted On : 2015-12-29 15:25:03.0
profile rahul patil - anyforum.in rahul patil
42-30
up-rate
3
down-rate

Answers


you can try the below code:

public class ArrayOperation {
public static void main(String[] args) {
Integer intArray[]=new Integer[5];
intArray[0]=15;
intArray[1]=30;
intArray[2]=25;
intArray[3]=22;
intArray[4]=28;
int max=0;
int secondMax=0;
for(int i=0;i<intArray.length;i++){
if(intArray[i]>max){
secondMax=max;
max=intArray[i];
}
if(intArray[i]>secondMax && max>intArray[i]){
secondMax=intArray[i];
}
}
System.out.println("Second Largest Element is "+secondMax);
}

}

Posted On : 2015-12-30 23:39:39
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129558962
Reply This Thread
up-rate
4
down-rate

Hi Garima,
I think secondMax =max; statement is not required.

Posted On : 2016-01-06 19:12:54
Satisfied : 1 Yes  0 No
profile Vikram Gore - anyforum.in Vikram Gore
050
Reply This Thread
up-rate
5
down-rate
Comments
yes.. you are right.. we can remove this secondMax =max; statement.
profile Garima Gupta - anyforum.in Garima Gupta
596  1295  58962
Posted On :2016-01-08 00:38:58.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in