Wednesday, August 22, 2018

How to find second largest number in an integer array?


package com.altafjava.sln.test;

public class SecondLargestNumber {
public static void main(String[] args) {

int[] arr={5,20,8,1,9,2,14,34,11};
int largest=Integer.MIN_VALUE;
int secondLargest=Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++){
if(arr[i]>largest){
secondLargest=largest;
largest=arr[i];
}
}
System.out.println("Original Array = 5,20,8,1,9,2,14,34,11");
System.out.println("Largest No = "+largest);
System.out.println("Second Largest No = "+secondLargest);
}
}



How to find second largest number in an integer array?






Share this

0 Comment to "How to find second largest number in an integer array?"

Post a Comment