Skip to content

Commit 8b3b3b8

Browse files
authored
Update 2022-10-17-second-largest-element-in-an-array.md
1 parent 2deb2b7 commit 8b3b3b8

File tree

1 file changed

+61
-61
lines changed

1 file changed

+61
-61
lines changed

‎_posts/java-programs/2022-10-17-second-largest-element-in-an-array.md

+61-61
Original file line numberDiff line numberDiff line change
@@ -11,72 +11,72 @@ In This tutorial, we will see a Java program to find the second largest element
1111

1212
## Introduction
1313

14-
In the previous article, [Java Program To Find Largest Element In An Array (3 Ways)](/largest-element-in-an-array/), we have seen a few programs to find the largest element in an array. Today, we will see a program to find the second largest element in an array.
14+
In the previous article, [Java Program To Find Largest Element In An Array (3 Ways)](/largest-element-in-an-array/), we have seen a few programs to find the largest element in an array. Today, we will see a program to find the second-largest element in an array.
1515

1616
Here we will take an array of integers `arr` and please note that `arr` is not a sorted array. ex. `{2, 5, 9, 8, 11, 18, 13}`
1717

18-
## Java Program to find second largest element in an array
18+
## Java Program to find the second largest element in an array
1919

2020
### 1. Iterating over an array
2121

2222
We will iterate over an array using the `for` loop to find the second largest element in an array.
2323

2424
```java
2525
/**
26-
* A Java program to find the second largest number in an array
27-
* by iterating over an array using for loop.
26+
* A Java program to find the second-largest number in an array
27+
* by iterating over an array using a for loop.
2828
*
2929
* @author coderolls.com
3030
*/
3131
public class SecondLargestElementInArray {
3232

33-
public static void main(String[] args) {
34-
int[] arr = {2, 5, 9, 8, 11, 18, 13};
35-
36-
int secondLargest = getSecondLargest(arr);
37-
System.out.println("The second largest element in "
38-
+ "an array 'arr'is :"+ secondLargest);
39-
}
40-
41-
private static int getSecondLargest(int[] arr) {
42-
int n =arr.length;
43-
int largest =arr[0];
44-
int secondLargest = -1;
45-
46-
for(int i=0; i<n; i++) {
47-
if(arr[i]>largest) {
48-
//if you found the new largest,
49-
//copy current largest to second largest and
50-
//copy current element arr[i] to largest
51-
secondLargest = largest;
52-
largest = arr[i];
53-
}else if(arr[i]!=largest) {
54-
// if the current element arr[i] is not the largest and
55-
// still larger than the current secondLargest
56-
// then copy it to secondLargest
57-
if(arr[i]>secondLargest) {
58-
secondLargest = arr[i];
59-
}
60-
}
61-
}
62-
return secondLargest;
63-
}
33+
public static void main(String[] args) {
34+
int[] arr = {2, 5, 9, 8, 11, 18, 13};
35+
36+
int secondLargest = getSecondLargest(arr);
37+
System.out.println("The second largest element in "
38+
+ "an array 'arr'is: "+ secondLargest);
39+
}
40+
41+
private static int getSecondLargest(int[] arr) {
42+
int n =arr.length;
43+
int largest =arr[0];
44+
int secondLargest = -1;
45+
46+
for(int i=0; i<n; i++) {
47+
if(arr[i]>largest) {
48+
//if you found the new largest,
49+
//copy current largest to second largest and
50+
//copy current element arr[i] to largest
51+
secondLargest = largest;
52+
largest = arr[i];
53+
}else if(arr[i]!=largest) {
54+
// if the current element arr[i] is not the largest and
55+
// still larger than the current secondLargest
56+
// then copy it to secondLargest
57+
if(arr[i]>secondLargest) {
58+
secondLargest = arr[i];
59+
}
60+
}
61+
}
62+
return secondLargest;
63+
}
6464
}
6565
```
6666

6767
Output:
6868

6969
```
70-
The second largest element in an array 'arr' is :13
70+
The second largest element in an array 'arr' is: 13
7171
```
7272

7373
#### Explanation:
7474

75-
1. In the main method, we have taken a sample array `arr = {2, 5, 9, 8, 11, 18, 13};` and passed it as a parameter to `getSecondLargest()` method to return the largest number in an array `arr`.
75+
1. In the main method, we have taken a sample array `arr = {2, 5, 9, 8, 11, 18, 13};` and passed it as a parameter to the `getSecondLargest()` method to return the largest number in an array `arr`.
7676

77-
2. In `getSecondLargest()` method we have stored the length of an array `arr` into int variable n using the `arr.length` method. To start the program we have assigned the number at index `0` i.e. `arr[0]` as the current largest number i.e `largest`. Also, we have assigned the value `-1` the current second largest number `secondLargest`.
77+
2. In the `getSecondLargest()` method we have stored the length of an array `arr` into int variable n using the `arr.length` method. To start the program we have assigned the number at index `0` i.e. `arr[0]` as the current largest number i.e. `largest`. Also, we have assigned the value `-1` the current second largest number `secondLargest`.
7878

79-
> If the array contains all the similar numbers, there will be no second largest number in an array. ex. `arr= {12,12,12,12}` So it will return -1 in that case.
79+
> If the array contains all the similar numbers, there will be no second-largest number in an array. ex. `arr= {12,12,12,12}` So it will return -1 in that case.
8080
8181
3. Using the `if` statement we are checking if the current number at index `i` i.e. `arr[i]` is larger than the current largest number i.e `largest`, we will
8282

@@ -89,45 +89,45 @@ The second largest element in an array 'arr' is :13
8989

9090
### 2. Using `Arrays.sort()`
9191

92-
As we know the array is not sorted, we can sort it using the `Arrays.sort()` method in natural sorting order. So the second last element of an array will be the second largest element of an array.
92+
As the array is not sorted, we can sort it using the `Arrays.sort()` method in natural sorting order. So the second last element of an array will be the second largest element of an array.
9393

9494
```java
9595
import java.util.Arrays;
9696

9797
/**
98-
* A Java program to find the second largest number in an array
98+
* A Java program to find the second-largest number in an array
9999
* using Arrays.sort() method.
100100
*
101101
* @author coderolls.com
102102
*/
103-
public class SecondLargestElementInArrayUsingArrays {
104-
105-
public static void main(String[] args) {
106-
int[] arr = {2, 5, 9, 8, 11, 18, 13};
107-
108-
int secondLargest = getSecondLargest(arr);
109-
System.out.println("The second largest element in"
110-
+ "an array 'arr' is using Arrays.sort() :"+ secondLargest);
111-
}
112-
113-
private static int getSecondLargest(int[] arr) {
114-
Arrays.sort(arr);
115-
// return second largest, so length-2
116-
return arr[arr.length-2];
117-
}
103+
public class SecondLargestElementInArrayUsingArrays {
104+
105+
public static void main(String[] args) {
106+
int[] arr = {2, 5, 9, 8, 11, 18, 13};
107+
108+
int secondLargest = getSecondLargest(arr);
109+
System.out.println("The second largest element in"
110+
+ "an array 'arr' is using Arrays.sort() :"+ secondLargest);
111+
}
112+
113+
private static int getSecondLargest(int[] arr) {
114+
Arrays.sort(arr);
115+
// return second largest, so length-2
116+
return arr[arr.length-2];
117+
}
118118
}
119119
```
120120

121121
Output:
122122

123123
```
124-
The second largest element in an array 'arr' is using Arrays.sort() :13
124+
The second largest element in an array 'arr' is using Arrays.sort(): 13
125125
```
126126

127127
#### Explanation:
128128

129-
1. In the main method, we have taken a sample array `arr = {2, 5, 9, 8, 11, 18, 13};` and passed it as a parameter to `getSecondLargest()` method to return the largest number in an array `arr`.
130-
2. In `getSecondLargest()` method, we have sorted an array `arr` in natural sorting order using the `Arrays.sort()` method.
129+
1. In the main method, we have taken a sample array `arr = {2, 5, 9, 8, 11, 18, 13};` and passed it as a parameter to the `getSecondLargest()` method to return the largest number in an array `arr`.
130+
2. In the `getSecondLargest()` method, we have sorted an array `arr` in natural sorting order using the `Arrays.sort()` method.
131131
3. Once we sort the array in natural sorting order, we will have the second largest number as the second last element of the array. We can get the second last number of an array as `arr[arr.length-2]` to return it.
132132

133133
> In this way ( 2. Using `Arrays.sort()`) even if all the numbers of an array are similar ex. `arr= {12,12,12,12}`, i.e. when there is no second largest element, it will return that same number.
@@ -136,12 +136,12 @@ The second largest element in an array 'arr' is using Arrays.sort() :13
136136

137137
We can find the second largest element in an array in the following two ways,
138138

139-
1. By iterating over an array using for loop to compare the largest and second largest number.
139+
1. By iterating over an array using a for loop to compare the largest and second largest number.
140140
2. By sorting an array in a natural sorting order and returning the second last element. i.e. `arr[arr.length-2]`
141141

142142
---
143143

144-
The example java programs used in the above article can be found at this GitHub repository, [blogpost-coding-examples/java-programs/second-largest-element-in-an-array/](https://github.com/coderolls/blogpost-coding-examples/tree/main/java-programs/second-largest-element-in-an-array).
144+
The example Java programs used in the above article can be found at this GitHub repository, [blogpost-coding-examples/java-programs/second-largest-element-in-an-array/](https://github.com/coderolls/blogpost-coding-examples/tree/main/java-programs/second-largest-element-in-an-array).
145145

146146
Please write your thoughts in the comment section below.
147147

0 commit comments

Comments
 (0)