Write a Java Program to Find the Second Largest Number in an Array

Write a Java Program to Find the Second Largest Number in an Array thumbnail
By Dhiraj Ray 10 March, 2018

Program Description

Write a java program to find second largest number in array.There are multiple ways to achieve this. We can sort any given array in a descending order and pick the second index element.The main concept here is to sort the given array.This can be achieved via Arrays.sort() or Collection.sort() and once the given array is sorted the second largest number can be easily found.

package com.devglan.set2;

public class SecondLargestInArray {

    public int findSecondLargestNumberInArray(int[] a){
        int temp;
        for (int i = 0; i < a.length; i++){
            for (int j = i + 1; j < a.length; j++){
                if (a[i] > a[j]){
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
        return a[a.length - 2];

    public static void main(String[] args){
        SecondLargestInArray array = new SecondLargestInArray();
        int a[]={4,7,55,16,33,27};
        System.out.println("Second largest number in given array is " + array.findSecondLargestNumberInArray(a));


Sort the array first in descending order and then find the second element of the array.


If You Appreciate This, You Can Consider:

We are thankful for your never ending support.

About The Author

A technology savvy professional with an exceptional capacity to analyze, solve problems and multi-task. Technical expertise in highly scalable distributed systems, self-healing systems, and service-oriented architecture. Technical Skills: Java/J2EE, Spring, Hibernate, Reactive Programming, Microservices, Hystrix, Rest APIs, Java 8, Kafka, Kibana, Elasticsearch, etc.