Home > Algorithms > Find Secondlargest number in an Array. Also find the index where it is there in array

Find Secondlargest number in an Array. Also find the index where it is there in array

July 7, 2012

private static int findSecondLargest(int[] integers) {

int length = integers.length;
int max = integers[0];
int secondMax = integers[0];

for(int i=1;i

if(integers[i]>max) {

secondMax = max;
max = integers[i];

}
else if(integers[i]>secondMax && integers[i]!=max){

secondMax = integers[i];
}

}
return secondMax;
}
Advertisements
Categories: Algorithms
%d bloggers like this: