
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Difference between first and the second array in JavaScript
- C++ Program to find the second largest element from the array
- Swift Program to find the second largest element from the array
- How to find the second largest element in a user-input JavaScript array?
- Difference between the largest and the smallest primes in an array in Java
- Find the Second Largest Element in a Linked List in C++
- Maximum difference between first and last indexes of an element in array in C
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Difference Between First level cache and Second level cache in Hibernate
- C program to find the second largest and smallest numbers in an array
- Largest difference between element with a twist in JavaScript
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Python Program to find largest element in an array
- Find the second most frequent element in array JavaScript
