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

• 11-02-2013, 10:54 PM
anjijava16
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}
• 11-02-2013, 10:59 PM
jim829
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
• 11-02-2013, 11:08 PM
anjijava16
Re: how to find 2nd largest array if array values like{10,20,92,81,92,34}
Quote:

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
• 11-02-2013, 11:55 PM
jim829
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
• 11-02-2013, 11:58 PM
Norm
Re: how to find 2nd largest array if array values like{10,20,92,81,92,34}