# Thread: how to find 2nd largest array if array values like{10,20,92,81,92,34}

1. Member
Join Date
Nov 2013
Posts
2
Rep Power
0

## how to find 2nd largest array if array values like{10,20,92,81,92,34}

how to find 2nd largest array if array values like{10,20,92,81,92,34}

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,642
Rep Power
9

## Re: how to find 2nd largest array if array values like{10,20,92,81,92,34}

Where is your code? Do you have a question?

Regards,
Jim

3. Member
Join Date
Nov 2013
Posts
2
Rep Power
0

## Re: how to find 2nd largest array if array values like{10,20,92,81,92,34}

Originally Posted by jim829
Where is your code? Do you have a question?

Regards,
Jim
package com.usr.arrays;

public class SecondLargestNumberInArray {
public static void main(String[] args) {
int arr[] = { 1, 23, 92, 81, 92, 88};//, 52, 48, 56, 66, 65, 76, 71, 85, 49, 53, 56, 61, 65, 84 };
secondLargeNumber(arr);
}

public static void secondLargeNumber(int[] arr) {
int largest = arr[0];
int secondLargest = arr[0];
System.out.println(arr.length);
System.out.println(largest);
for (int i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];

} else if (arr[i] > secondLargest&secondLargest!=largest) {
secondLargest = arr[i];

}
/*if(arr[i]<largest){
secondLargest = largest;
largest = arr[i];

} else if (arr[i] > secondLargest) {
secondLargest = arr[i];

}*///For 1st Largest Number

}
System.out.println("second largest in array is:" + secondLargest);

}
}
==============================
In This case my array contain 2 values are same

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,642
Rep Power
9

## Re: how to find 2nd largest array if array values like{10,20,92,81,92,34}

Try approaching it from the perspective of already knowing the largest value.

Regards,
Jim

5. ## Re: how to find 2nd largest array if array values like{10,20,92,81,92,34}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•