# Thread: Print the second Largest in an array

1. Member
Join Date
Sep 2009
Posts
14
Rep Power
0

## 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

Java 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]);
}

}```

2. The complicated way or the simple way?
Java 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]);```

3. 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

4. I didn't learn programming on stone tablets like you ;-)

5. 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 ;-)

6. Member
Join Date
Sep 2009
Posts
14
Rep Power
0
Originally Posted by PhHein
The complicated way or the simple way?
Java 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);

7. @Jos:
Bare feet in snow, uphill, both ways...
Oh, no, that was something else ;-)

8. 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

9. Member
Join Date
Sep 2009
Posts
14
Rep Power
0
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

10. 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 ;-)

11. 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:
Java Code:
```Arrays.sort(arr);
System.out.println(Arrays.toString(arr));```

12. Member
Join Date
Sep 2009
Posts
14
Rep Power
0
Originally Posted by PhHein
What is a hard code approach? To print you can use this lazy approach:
Java Code:
```Arrays.sort(arr);
System.out.println(Arrays.toString(arr));```
Here is my revised code, but it doesn't give the correct results;

Java 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

13. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27
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.

14. 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:

Java 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:

Java 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:

Java 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

15. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27
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...:)

16. 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

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

17. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27
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...

18. 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

19. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27
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.

20. 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.
Last edited by JosAH; 05-05-2010 at 10:42 AM.

Page 1 of 2 12 Last