# Print the second Largest in an array

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-04-2010, 03:25 PM
singularity
Print the second Largest in an array
Hi, I need to print the second largest element of array. I have written the code to print the first largest, can anyone guide me how do I modify it so that it prints second largest instead

Code:

```public class SecondLargest {         public static void main(String args[]) {                 int temp=0,i=0;                 int arr[] = {277,3,444,78};                 for(i =0;i<arr.length-1; i++) {                                                 if(arr[i] > arr[i+1]) {                                 temp = arr[i];                                 arr[i] = arr[i+1];                                 arr[i+1] = temp;                         }                                         }                 System.out.println("Largest is :" +arr[i]);         } }```
• 05-04-2010, 03:34 PM
PhHein
The complicated way or the simple way?
Code:

```int arr[] = {277,3,444,78}; Arrays.sort(arr); System.out.println("largest: " + arr[arr.length-1]); System.out.println("second largest: " + arr[arr.length-2]);```
• 05-04-2010, 04:44 PM
JosAH
Quote:

Originally Posted by PhHein
The complicated way or the simple way?

s/simple/lazy/ It doesn't take rocket science to find the largest number and the next to largest number in O(n) where 'n' is the length of the sequence of the numbers. Your awfully lazy method takes O(n*log(n)) steps; shame on you ;-)

kind regards,

Jos
• 05-04-2010, 04:50 PM
PhHein
I didn't learn programming on stone tablets like you ;-)
• 05-04-2010, 05:06 PM
JosAH
Quote:

Originally Posted by PhHein
I didn't learn programming on stone tablets like you ;-)

Aah, those were the days: we had to carve those bits in stone with our teeth, twentynine hours per day but we were lucky then ...

kind regards,

Jos ;-)
• 05-04-2010, 05:06 PM
singularity
Quote:

Originally Posted by PhHein
The complicated way or the simple way?
Code:

```int arr[] = {277,3,444,78}; Arrays.sort(arr); System.out.println("largest: " + arr[arr.length-1]); System.out.println("second largest: " + arr[arr.length-2]);```

Thanks for the help, but btw how do I find second largest element using hard code approach, and how can I print sorted elements using Arrays.sort(arr);
• 05-04-2010, 05:07 PM
PhHein
@Jos:
Bare feet in snow, uphill, both ways...
Oh, no, that was something else ;-)
• 05-04-2010, 05:08 PM
JosAH
Quote:

Originally Posted by singularity
Thanks for the help, but btw how do I find second largest element using hard code approach, and how can I print sorted elements using Arrays.sort(arr);

Can you print all elements of an array? If so you can print all elements of a sorted array.

kind regards,

Jos
• 05-04-2010, 05:08 PM
singularity
Quote:

Originally Posted by JosAH
s/simple/lazy/ It doesn't take rocket science to find the largest number and the next to largest number in O(n) where 'n' is the length of the sequence of the numbers. Your awfully lazy method takes O(n*log(n)) steps; shame on you ;-)

kind regards,

Jos

Do you mean PhHein code is less optimized than doing it hard code way
• 05-04-2010, 05:11 PM
JosAH
Quote:

Originally Posted by singularity
Do you mean PhHein code is less optimized than doing it hard code way

Yes, he is a sissy; us real programmers, we had it rough.

kind regards,

Jos ;-)
• 05-04-2010, 05:12 PM
PhHein
Quote:

Originally Posted by singularity
Thanks for the help, but btw how do I find second largest element using hard code approach, and how can I print sorted elements using Arrays.sort(arr);

What is a hard code approach? To print you can use this lazy approach:
Code:

```Arrays.sort(arr); System.out.println(Arrays.toString(arr));```
• 05-04-2010, 05:19 PM
singularity
Quote:

Originally Posted by PhHein
What is a hard code approach? To print you can use this lazy approach:
Code:

```Arrays.sort(arr); System.out.println(Arrays.toString(arr));```

Here is my revised code, but it doesn't give the correct results;

Code:

```public class SecondLargest {         public static void main(String args[]) {                 int temp=0,i=0;                 int arr[] = {277,3,444,78,45,55,23,30,98,12,87};                 for(i =0;i<arr.length-1; i++) {                                                 if(arr[i] > arr[i+1]) {                                 temp = arr[i];                                 arr[i] = arr[i+1];                                 arr[i+1] = temp;                         }                                         }                 System.out.println("Largest is :" +arr[i]);                 System.out.println("SORTED ARRAY IS:" +Arrays.toString(arr));                 System.out.println("The largest array element is: " +arr[arr.length-1]);                 System.out.println("The second largest element is: " +arr[arr.length-2]);         }         }```

what is wrong and thanks for the help

by hard code I mean what I have written to find largest element, I am asking how to find the second largest using the same approach
• 05-04-2010, 05:32 PM
Tolls
Store a temp2, with the second largest in it...?

You should be able to figure out how you use that if you've already done this with the largest.
• 05-04-2010, 05:46 PM
JosAH
Quote:

Originally Posted by singularity
Here is my revised code, but it doesn't give the correct results;

I don't know what you're doing there in that code but let's solve this in a more or less OO way. First we design something that we can feed ints and it remembers the largest number we've ever fed it; here it is:

Code:

```class KeeperOfTheLargest {   private int largest= Integer.MIN_VALUE; // largest number so far   public void feed(int number) { // feed it another number       if (number > largest)         largest= number;   }   public int getLargest() { // get the largest number (sofar)       return largest;   } }```
... that's all there is to it; the following code snippet finds the largest number in an array:

Code:

```KeeperOfTheLargest kotl= new KeeperOfTheLargest(); int[] array= ... for (int i= 0; i < array.length; i++)   kotl.feed(array[i]); int largest= kotl.getLargest();```
... we have found the largest number in the array; the next snippet finds the next to largest number:

Code:

```KeeperOfTheLargest sl= new KeeperOfTheLargest(); for (int i= 0; i < array.length; i++)   if (array[i] != largest)       sl.feed(array[i]); int second= sl.getLargest();```
... Now you have both numbers: largest and second. There are other ways to skin this cat though.

kind regards,

Jos
• 05-04-2010, 05:51 PM
Tolls
Pah...two passes?
:D

And, of course, Jos is only leaving out the curly brackets he would usually put in for the loop and if for brevity...:)
• 05-04-2010, 06:16 PM
JosAH
Quote:

Originally Posted by Tolls
Pah...two passes?
:D

Yep, and it's a O(n) algorithm but it hasn't all those twisted, nested if-then-else statements :-P

Quote:

And, of course, Jos is only leaving out the curly brackets he would usually put in for the loop and if for brevity...:)
Actually I didn't, I find those curly braces around a single statement silly; I've been programming in BCPL, C, C++, Objective C, Java for many moons now and almost never made a mistake, YMMV.

kind regards,

Jos
• 05-05-2010, 09:00 AM
Tolls
Ah, but (as with most things IT) it's not about you making a mistake, but the someone who comes after you making one. And a lot of minor fixing and maintenance work is done by junior developers...
• 05-05-2010, 09:48 AM
JosAH
Quote:

Originally Posted by Tolls
Ah, but (as with most things IT) it's not about you making a mistake, but the someone who comes after you making one. And a lot of minor fixing and maintenance work is done by junior developers...

I couldn't care less: we have source code 'beautifiers' (formatters) so that the source code always is stored in the repositories (git, monotone, cvs, whatever) in the same format. When I work on source code I simply never add those braces around a single statement, I'm used to doing it that way. The way it shows up after formatting is not my concern (and I don't care either). I've seen juniors ruin my code more than often, not because of my lack of braces but because they didn't understand how the thing worked; to me commenting the stuff is much more important.

kind regards,

Jos
• 05-05-2010, 10:28 AM
Tolls
I suppose that's the difference.
You work in one place...I work all over the place.
Not everywhere has code beautifiers, as you put it...consequently I have to care about what goes into a clients repository.
• 05-05-2010, 10:39 AM
JosAH
Quote:

Originally Posted by Tolls
I suppose that's the difference.
You work in one place...I work all over the place.
Not everywhere has code beautifiers, as you put it...consequently I have to care about what goes into a clients repository.

I was self employed too but I always refused to deliver one snippet of code if they didn't have a code repository; I might as well have thrown my code in the ditch then. You can keep a repository yourself; I use git, it's open source and very flexible, but monotone is a nice one too.

kind regards,

Jos

edit: for those code beautifiers: have a look at Jalopy, it works fine and it's free. You can install it as a 'filter' in git, before your code enters the repository.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last