Showing posts with label Highest Element in Array. Show all posts
Showing posts with label Highest Element in Array. Show all posts

Saturday, August 18, 2012

Finding second largest element in array using java


public class SecondHighestElement {

public static void main(String args[]){

int a[]={1,2,46,672,24,5,6,46,672};

System.out.println("Second Highest Element"+secHighestElement(a));
}

private static int  secHighestElement(int[] a) {
int first=0,second=0;

for(int i=0;i<a.length;i++){
if (first <a[i]){
second=first;
first=a[i];
}
if(second<a[i] && first!=a[i])
second=a[i];
}
return second;
}
}