Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

write java code to print second max number in the array

Answer Posted / rajesh s

This is I tried.
public class SecondLargestNumber {
/**
* @param args
*/
public static void main(String[] args) {
int[] num = new int[]{-101,-105,-2,-7,-22,-
104,-8,-10,-100,-102,-102};
int big=-1;
int secbig=-1;
if(num.length == 1) {
big = num[0];
secbig = num[0];
} else {
if(num[0] > num[1]) {
big = num[0];
secbig = num[1];
} else {
big = num[1];
secbig = num[0];
}
}
if(num.length > 2){
for(int i=2;i<num.length;++i){
if(num[i] > secbig && num
[i] <big) {
secbig = num[i];
}
if(num[i]>=big){
secbig = big;
big = num[i];
}
}
}
System.out.println(big);
System.out.println(secbig);
}
}

Please check is it working and let me know. For removing
duplicates, we will have one more method to remove the
duplicate elements.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we execute java program without main method?

1023


What is the latest version of java?

1065


Difference between current previous versions of Java?

981


How would you convert bytes to string?

944


Does constructor return any value?

981


What are the common uses of "this" keyword in java ?

972


What is the difference between keyword and identifier?

983


Define canvas?

924


What are the uses of synchronized keyword?

959


Given a singly linked list, determine whether it contains a loop or not without using temporary space?

942


What happens when heap memory is full?

929


Can we have a method name same as class name in java?

1002


What is meant by JVM? Is JVM platform independent or not?

1015


Difference between throw and throws?

1055


Why java is call by value?

984