How to calculate Third Largest numbet in java from a Unsorted Array

Count Number of characters in a Given String
SOURCE : ThirdLargest.java


public class ThirdLargest {

public static void main(String[] args) {

int[] num = { 0, -87, 65, 87, 98, 76, 56 };

ThirdLargest tt = new ThirdLargest();
tt.max(num);

}

private void max(int[] num) {

int maxone = 0;
int maxtwo = 0;
int maxthree = 0;

try {
for (int n : num) {
if (maxone < n) {
maxthree = maxtwo;
maxtwo = maxone;
maxone = n;

} else if (maxtwo < n) {
maxthree = maxtwo;
maxtwo = n;

} else if (maxthree < n) {
maxthree = n;
}

}
} catch (Exception e) {
// TODO: handle exception
}

System.out.println("1st largest=:" + maxone);
System.out.println("2nd largest=" + maxtwo);
System.out.println("3rd largest=:" + maxthree);
}
}




OUTPUT

1st largest=:98
2nd largest=87
3rd largest=:76







Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment