You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/java-programs/2022-10-17-second-largest-element-in-an-array.md
+61-61
Original file line number
Diff line number
Diff line change
@@ -11,72 +11,72 @@ In This tutorial, we will see a Java program to find the second largest element
11
11
12
12
## Introduction
13
13
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 secondlargest 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.
15
15
16
16
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}`
17
17
18
-
## Java Program to find second largest element in an array
18
+
## Java Program to find the second largest element in an array
19
19
20
20
### 1. Iterating over an array
21
21
22
22
We will iterate over an array using the `for` loop to find the second largest element in an array.
23
23
24
24
```java
25
25
/**
26
-
* A Java program to find the secondlargest 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.
28
28
*
29
29
* @author coderolls.com
30
30
*/
31
31
publicclassSecondLargestElementInArray {
32
32
33
-
publicstaticvoidmain(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
-
privatestaticintgetSecondLargest(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
-
}elseif(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
+
publicstaticvoidmain(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
+
privatestaticintgetSecondLargest(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
+
}elseif(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
+
}
64
64
}
65
65
```
66
66
67
67
Output:
68
68
69
69
```
70
-
The second largest element in an array 'arr' is :13
70
+
The second largest element in an array 'arr' is: 13
71
71
```
72
72
73
73
#### Explanation:
74
74
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`.
76
76
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`.
78
78
79
-
> If the array contains all the similar numbers, there will be no secondlargest 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.
80
80
81
81
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
82
82
@@ -89,45 +89,45 @@ The second largest element in an array 'arr' is :13
89
89
90
90
### 2. Using `Arrays.sort()`
91
91
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.
93
93
94
94
```java
95
95
importjava.util.Arrays;
96
96
97
97
/**
98
-
* A Java program to find the secondlargest number in an array
98
+
* A Java program to find the second-largest number in an array
System.out.println("The second largest element in"
110
+
+"an array 'arr' is using Arrays.sort() :"+ secondLargest);
111
+
}
112
+
113
+
privatestaticintgetSecondLargest(int[] arr) {
114
+
Arrays.sort(arr);
115
+
// return second largest, so length-2
116
+
return arr[arr.length-2];
117
+
}
118
118
}
119
119
```
120
120
121
121
Output:
122
122
123
123
```
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
125
125
```
126
126
127
127
#### Explanation:
128
128
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.
131
131
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.
132
132
133
133
> 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
136
136
137
137
We can find the second largest element in an array in the following two ways,
138
138
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.
140
140
2. By sorting an array in a natural sorting order and returning the second last element. i.e. `arr[arr.length-2]`
141
141
142
142
---
143
143
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).
145
145
146
146
Please write your thoughts in the comment section below.
0 commit comments