Q. Write a program to find Second largest number in the array?
Ans: Method:-
public static void secondLargestNumber(int arr){
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++){
if(arr[i] > first){
second = first;
first = arr[i];
}else if(arr[i] > second){
second = arr[i];
}
}
System.out.println(" Second Largest Number = " + second);
}
No comments:
Post a Comment