Find the second largest number in the array

In this Post, I am going to discuss how to find the second largest number in the array.

So, without wasting time, let’s start

I am going to discuss the problem by using Java Collections. So, it is good for you to have knowledge of Java collections.

There is no any prerequisite for this problem except some patience to read. Have a copy and pen in your hand and start reading.

Problem

Given an array of integers, our task is to write a program that efficiently finds the second largest element present in the array.

Input – arr = [1,23,43,52,11];

output – second largest element is 43.

Input – arr =[23,22,56,76,8,123]

output – second largest element is 76

Solution – 1

This is the simplest method, all you need to sort the given element and return the second index if sorted in descending order or return second last index if sorted in ascending order

Time Complexity – O(n log(n))

Example – arr = [1,23,4,56,7];

Sorted arr = [1,4,7,23,56] we return arr[3] as our answer.

Program –

Note: No any element is repeated, If the element is repeated firstly add each element in sets then sort.

// { Driver Code Starts
//Initial Template for Java


import java.io.*;
import java.util.*;


class Array {
	public static void main (String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int t = sc.nextInt();
        while(t-- > 0)
        {
            int n = sc.nextInt();
            int a[] = new int[n + 1];
            
            for(int i=0;i<n;i++){
                a[i] = sc.nextInt();
            }
            
            Solution ob = new Solution();
            
            System.out.println(ob.print2largest(a,n));
        }
        
	}
}// } Driver Code Ends


class Solution
{ 
    public static int print2largest(int A[],int N) 
    {
        
        int arr[] = new int[N]; 
        for(int  i = 0;i<N; i++)
        arr[i] =  A[i];
        Arrays.sort(arr);
        return arr[N-2];
        
     
    }
}
    

Solution – 2

In this method, first we find the greatest element by single traversal and then we again traversed the whole array and then we will find the second greatest element

Algorithm

  1. Traverse the complete array and find first greatest element
  2. Again traverse the array and find second greatest element.
// Java program to find second largest 
// element in an array 
class GFG{ 

// Function to print the second largest elements 
static void print2largest(int arr[], int arr_size) 
{ 
	int i, first, second; 

	// There should be atleast two elements 
	if (arr_size < 2) 
	{ 
		System.out.printf(" Invalid Input "); 
		return; 
	} 

	int largest = second = Integer.MIN_VALUE; 

	// Find the largest element 
	for(i = 0; i < arr_size; i++) 
	{ 
		largest = Math.max(largest, arr[i]); 
	} 

	// Find the second largest element 
	for(i = 0; i < arr_size; i++) 
	{ 
		if (arr[i] != largest) 
			second = Math.max(second, arr[i]); 
	} 
	if (second == Integer.MIN_VALUE) 
		System.out.printf("There is no second " + 
						"largest element\n"); 
	else
		System.out.printf("The second largest " + 
						"element is %d\n", second); 
} 

// Driver code 
public static void main(String[] args) 
{ 
	int arr[] = { 12, 35, 1, 10, 34, 1 }; 
	int n = arr.length; 
	
	print2largest(arr, n); 
} 
} 

This is the two ways to find the second largest number in the array. Comment in case of any queries.

Leave a Comment

Your email address will not be published. Required fields are marked *