# Find difference between first and second largest array element in Java

In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to find the difference between the first largest and second largest number present in an array.

Note − The array must be an integer array.

Let’s explore the article to see how it can be done by using Java programming language.

### To Show You Some Instances

#### Instance-1

Suppose the original array is {22, 45, 1, 10, 52, 27}


After performing the operation, the result will be −

The second largest element of an array is − 45

The largest element of an array is − 52

The difference of largest and second largest element of an array is − 7

#### Instance-2

Suppose the original array is {5, 41, 11, 21, 32, 27}


After performing the operation the result will be −

The second largest element of an array is − 32

The largest element of an array is − 41

The difference of largest and second largest element of an array is − 9

#### Instance-3

Suppose the original array is {18, 25, 61, 19, 55, 29}


After performing the operation the result will be −

The second largest element of an array is − 55

The largest element of an array is − 61

The difference of largest and second largest element of an array is − 6

### Algorithm

• Step 1 − Declare and initialize an integer array.

• Step 2 − Sort an array in ascending order.

• Step 3 − Get the second largest and largest number of an array using “arr[n - 2]” and “arr[n - 1]”.

• Step 4 − Subtract the largest and second largest element.

• Step 5 − Print the number.

### Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

array.length


where, ‘array’ refers to the array reference.

To sort the array, The Arrays class of java.util package provides an inbuilt sort() method.

Below refers to the syntax of it −

Arrays.sort(arr);


By default it sorts the array in ascending order.

where, ‘arr’ refers to the array reference.

### Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Initialization of Array

• By Using User Defined Method

Let’s see the program along with its output one by one.

## Approach-1: By Using Static Initialization of Array

### Example

In this approach, array elements will be initialized in the program. Then as per the algorithm find the difference between the first largest and second largest number present in an array.

import java.util.*;
public class Main {

//main method
public static void main(String[] args) {

//Declare and initialize the array elements
int arr[] = {5, 41, 11, 21, 32, 27};

//get length of an array
int n = arr.length;

//sorting an array in ascending order
Arrays.sort(arr);

//getting the second largest number of an array
int p = arr[n - 2];

//print the second largest number of an array
System.out.println("The second largest element of an array is: " + p);

//sorting an array in ascending order
Arrays.sort(arr);

//getting the largest number of an array
int q = arr[n - 1];

//print the largest number of an array
System.out.println("The largest element of an array is: " + q);

//finding the difference between largest and second largest number
int r = q - p;
System.out.println("The difference of largest and second largest element of an array is: " + r);
}
}


### Output

The second largest element of an array is: 32
The largest element of an array is: 41
The difference of largest and second largest element of an array is: 9


## Approach-2: By Using User Defined Method

### Example

In this approach, array elements will be initialized in the program. Then call a user-defined method by passing the array as a parameter and inside the method as per the algorithm find the difference between the first largest and second largest number present in an array.

import java.util.*;
public class Main{

//main method
public static void main(String[] args) {

//Declare and initialize the array elements
int arr[] = {18, 25, 61, 19, 55, 29};

//calling user defined method
diff(arr);
}

//user defined method
static void diff(int []arr){

//get length of an array
int n = arr.length;

//sorting an array in ascending order
Arrays.sort(arr);

//getting the second largest number of an array
int p = arr[n - 2];

//print the second largest number of an array
System.out.println("The second largest element of an array is: " + p);

//sorting an array in ascending order
Arrays.sort(arr);

//getting the largest number of an array
int q = arr[n - 1];

//print the largest number of an array
System.out.println("The largest element of an array is: " + q);

//finding the difference between largest and second largest number
int r = q - p;
System.out.println("The difference of largest and second largest element of an array is: " + r);
}
}


### Output

The second largest element of an array is: 55
The largest element of an array is: 61
The difference of largest and second largest element of an array is: 6


In this article, we explored how to find the difference between first largest and second largest number present in an array by using Java programming language.