Java Code to find Second Largest Number from Array. 20:29 Add Comment Edit Find Second Largest number in java : import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.io.*; public class Demo { public static void main(String args[]) throws Exception { // Write code here Scanner in = new Scanner(System.in); int max, smax, n; n = in.nextInt(); int a[] = new int[n]; max = a[0]; smax = a[0]; for (int i = 0; i < a.length; i++) { a[i] = in.nextInt(); } for (int i = 0; i < a.length; i++) { if (a[i] > max) { smax = max; max = a[i]; } else if (a[i] > smax) { smax = a[i]; } } System.out.println(smax); } } Share this:
0 comments:
Post a Comment