# How to get largest and second largest in an array of integer.

Page 1 of 1

## 7 Replies - 17333 Views - Last Post: 30 January 2013 - 10:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=310382&amp;s=83000f5712ea3139c77ce1d3e9b6364e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 RozenKristal

• D.I.C Regular

Reputation: 45
• Posts: 381
• Joined: 29-September 12

# How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 04:22 PM

I just want to ask what should be the way to do it, not asking for code.
So currently, I declare 2 variables: largest and second largest.
I go thru the array of integer, and state:
```if (array[i] > largest){
largest = *array[i];} else if (array[i] > second largest) { second largest = *array[i]};
```

Is This A Good Question/Topic? 0

## Replies To: How to get largest and second largest in an array of integer.

### #2 andrewsw

• lashings of ginger beer

Reputation: 6340
• Posts: 25,565
• Joined: 12-December 12

## Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 05:35 PM

Sounds okay but 'second largest' cannot be two separate words - perhaps it is not in your actual code.

[I'm assuming you are initially setting largest, and secondLargest, to a small value.]

Mmm might have to check(?) - supposing the first element is the 2nd largest? It's late for me, so you'll need to check.

This post has been edited by andrewsw: 30 January 2013 - 05:44 PM

### #3 andrewsw

• lashings of ginger beer

Reputation: 6340
• Posts: 25,565
• Joined: 12-December 12

## Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 05:54 PM

I think on the very first iteration I would set both the largest and secondLargest to whatever the first value is. Subsequent iterations would use your code.

### #4 anonymous26

• D.I.C Lover

Reputation: 2
• Posts: 3,638
• Joined: 26-November 10

## Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 06:37 PM

You need a variant of selection sort. Google it, it's a very simple algorithm.

### #5 #define

• Duke of Err

Reputation: 1850
• Posts: 6,646
• Joined: 19-February 09

## Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 07:28 PM

If

```largest = 5
second largest = 4

next element = 6
```

What is the new largest and new second largest?

### #6 buffalobill

Reputation: 21
• Posts: 188
• Joined: 08-July 08

## Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 09:41 PM

Sort the int arr[] in descending order. arr[0] and arr[1] will be the two elements you want.

### #7 jjl

• Engineer

Reputation: 1265
• Posts: 4,979
• Joined: 09-June 09

## Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 09:55 PM

Quote

Sort the int arr[] in descending order. arr[0] and arr[1] will be the two elements you want.

Sorting the entire array is overkill. Just modify one of the existing sorting algorithms to get the 2 most greater elements.

This post has been edited by jjl: 30 January 2013 - 09:55 PM

### #8 anonymous26

• D.I.C Lover

Reputation: 2
• Posts: 3,638
• Joined: 26-November 10

## Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 10:54 PM

That is why I suggested selection sort.