Third largest element in an array of distinct elements
Last Updated :
17 Feb, 2025
Given an array of n integers, the task is to find the third largest element. All the elements in the array are distinct integers.
Examples :
Input: arr[] = {1, 14, 2, 16, 10, 20}
Output: 14
Explanation: Largest element is 20, second largest element is 16 and third largest element is 14
Input: arr[] = {19, -10, 20, 14, 2, 16, 10}
Output: 16
Explanation: Largest element is 20, second largest element is 19 and third largest element is 16
[Naive Approach] Using Sorting - O(n * log n) time and O(1) space
The idea is to sort the array and return the third largest element in the array which will be present at (n-3)'th index.
C++
// C++ program to find the third largest
// element in an array.
#include <bits/stdc++.h>
using namespace std;
int thirdLargest(vector<int> &arr) {
int n = arr.size();
// Sort the array
sort(arr.begin(), arr.end());
// Return the third largest element
return arr[n-3];
}
int main() {
vector<int> arr = {1, 14, 2, 16, 10, 20};
cout << thirdLargest(arr) << endl;
return 0;
}
Java
// Java program to find the third largest
// element in an array.
import java.util.*;
class GfG {
static int thirdLargest(int[] arr) {
int n = arr.length;
// Sort the array
Arrays.sort(arr);
// Return the third largest element
return arr[n - 3];
}
public static void main(String[] args) {
int[] arr = {1, 14, 2, 16, 10, 20};
System.out.println(thirdLargest(arr));
}
}
Python
# Python program to find the third largest
# element in an array.
# Function to find the third largest element
def thirdLargest(arr):
n = len(arr)
# Sort the array
arr.sort()
# Return the third largest element
return arr[n - 3]
if __name__ == "__main__":
arr = [1, 14, 2, 16, 10, 20]
print(thirdLargest(arr))
C#
// C# program to find the third largest
// element in an array.
using System;
class GfG {
static int thirdLargest(int[] arr) {
int n = arr.Length;
// Sort the array
Array.Sort(arr);
// Return the third largest element
return arr[n - 3];
}
static void Main() {
int[] arr = {1, 14, 2, 16, 10, 20};
Console.WriteLine(thirdLargest(arr));
}
}
JavaScript
// JavaScript program to find the third largest
// element in an array.
// Function to find the third largest element
function thirdLargest(arr) {
let n = arr.length;
// Sort the array
arr.sort((a, b) => a - b);
// Return the third largest element
return arr[n - 3];
}
let arr = [1, 14, 2, 16, 10, 20];
console.log(thirdLargest(arr));
[Expected Approach - 1] Using Three Loops - O(n) time and O(1) space
The idea is to iterate the array twice and mark the maximum and second maximum element and then excluding them both find the third maximum element, i.e., the maximum element excluding the maximum and second maximum.
Step by step approach:
- First, iterate through the array and find maximum.
- Store this as first maximum along with its index.
- Now traverse the whole array finding the second max, excluding the maximum element.
- Finally traverse the array the third time and find the third largest element i.e., excluding the maximum and second maximum.
C++
// C++ program to find the third largest
// element in an array.
#include <bits/stdc++.h>
using namespace std;
int thirdLargest(vector<int> &arr) {
int n = arr.size();
// Find the first maximum element.
int first = INT_MIN;
for (int i=0; i<n; i++) {
if (arr[i] > first) first = arr[i];
}
// Find the second max element.
int second = INT_MIN;
for (int i=0; i<n; i++) {
if (arr[i] > second && arr[i] < first) {
second = arr[i];
}
}
// Find the third largest element.
int third = INT_MIN;
for (int i=0; i<n; i++) {
if (arr[i] > third && arr[i] < second) {
third = arr[i];
}
}
// Return the third largest element
return third;
}
int main() {
vector<int> arr = {1, 14, 2, 16, 10, 20};
cout << thirdLargest(arr) << endl;
return 0;
}
Java
// Java program to find the third largest
// element in an array.
import java.util.*;
class GfG {
static int thirdLargest(int[] arr) {
int n = arr.length;
// Find the first maximum element.
int first = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
if (arr[i] > first) first = arr[i];
}
// Find the second max element.
int second = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
if (arr[i] > second && arr[i] < first) {
second = arr[i];
}
}
// Find the third largest element.
int third = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
if (arr[i] > third && arr[i] < second) {
third = arr[i];
}
}
// Return the third largest element
return third;
}
public static void main(String[] args) {
int[] arr = {1, 14, 2, 16, 10, 20};
System.out.println(thirdLargest(arr));
}
}
Python
# Python program to find the third largest
# element in an array.
# Function to find the third largest element
def thirdLargest(arr):
n = len(arr)
# Find the first maximum element.
first = float('-inf')
for i in range(n):
if arr[i] > first:
first = arr[i]
# Find the second max element.
second = float('-inf')
for i in range(n):
if arr[i] > second and arr[i] < first:
second = arr[i]
# Find the third largest element.
third = float('-inf')
for i in range(n):
if arr[i] > third and arr[i] < second:
third = arr[i]
# Return the third largest element
return third
if __name__ == "__main__":
arr = [1, 14, 2, 16, 10, 20]
print(thirdLargest(arr))
C#
// C# program to find the third largest
// element in an array.
using System;
class GfG {
static int thirdLargest(int[] arr) {
int n = arr.Length;
// Find the first maximum element.
int first = int.MinValue;
for (int i = 0; i < n; i++) {
if (arr[i] > first) first = arr[i];
}
// Find the second max element.
int second = int.MinValue;
for (int i = 0; i < n; i++) {
if (arr[i] > second && arr[i] < first) {
second = arr[i];
}
}
// Find the third largest element.
int third = int.MinValue;
for (int i = 0; i < n; i++) {
if (arr[i] > third && arr[i] < second) {
third = arr[i];
}
}
// Return the third largest element
return third;
}
static void Main() {
int[] arr = {1, 14, 2, 16, 10, 20};
Console.WriteLine(thirdLargest(arr));
}
}
JavaScript
// JavaScript program to find the third largest
// element in an array.
// Function to find the third largest element
function thirdLargest(arr) {
let n = arr.length;
// Find the first maximum element.
let first = -Infinity;
for (let i = 0; i < n; i++) {
if (arr[i] > first) first = arr[i];
}
// Find the second max element.
let second = -Infinity;
for (let i = 0; i < n; i++) {
if (arr[i] > second && arr[i] < first) {
second = arr[i];
}
}
// Find the third largest element.
let third = -Infinity;
for (let i = 0; i < n; i++) {
if (arr[i] > third && arr[i] < second) {
third = arr[i];
}
}
// Return the third largest element
return third;
}
let arr = [1, 14, 2, 16, 10, 20];
console.log(thirdLargest(arr));
[Expected Approach - 2] Using Three variables - O(n) time and O(1) space
The idea is to traverse the array from start to end and to keep track of the three largest elements up to that index (stored in variables). So after traversing the whole array, the variables would have stored the indices (or value) of the three largest elements of the array.
Step by step approach:
- Create three variables, first, second, third, to store indices of three largest elements of the array. (Initially all of them are initialized to a minimum value).
- Move along the input array from start to the end.
- For every index check if the element is larger than first or not. Update the value of first, if the element is larger, and assign the value of first to second and second to third. So the largest element gets updated and the elements previously stored as largest becomes second largest, and the second largest element becomes third largest.
- Else if the element is larger than the second, then update the value of second, and the second largest element becomes third largest.
- If the previous two conditions fail, but the element is larger than the third, then update the third.
C++
// C++ program to find the third largest
// element in an array.
#include <bits/stdc++.h>
using namespace std;
int thirdLargest(vector<int> &arr) {
int n = arr.size();
int first = INT_MIN, second = INT_MIN,
third = INT_MIN;
for (int i=0; i<n; i++) {
// If arr[i] is greater than first,
// set third to second, second to
// first and first to arr[i].
if (arr[i] > first) {
third = second;
second = first;
first = arr[i];
}
// If arr[i] is greater than second,
// set third to second and second
// to arr[i].
else if (arr[i] > second) {
third = second;
second = arr[i];
}
// If arr[i] is greater than third,
// set third to arr[i].
else if (arr[i] > third) {
third = arr[i];
}
}
// Return the third largest element
return third;
}
int main() {
vector<int> arr = {1, 14, 2, 16, 10, 20};
cout << thirdLargest(arr) << endl;
return 0;
}
Java
// Java program to find the third largest
// element in an array.
import java.util.*;
class GfG {
static int thirdLargest(int[] arr) {
int n = arr.length;
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE,
third = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
// If arr[i] is greater than first,
// set third to second, second to
// first and first to arr[i].
if (arr[i] > first) {
third = second;
second = first;
first = arr[i];
}
// If arr[i] is greater than second,
// set third to second and second
// to arr[i].
else if (arr[i] > second) {
third = second;
second = arr[i];
}
// If arr[i] is greater than third,
// set third to arr[i].
else if (arr[i] > third) {
third = arr[i];
}
}
// Return the third largest element
return third;
}
public static void main(String[] args) {
int[] arr = {1, 14, 2, 16, 10, 20};
System.out.println(thirdLargest(arr));
}
}
Python
# Python program to find the third largest
# element in an array.
# Function to find the third largest element
def thirdLargest(arr):
n = len(arr)
first, second, third = float('-inf'), float('-inf'), float('-inf')
for i in range(n):
# If arr[i] is greater than first,
# set third to second, second to
# first and first to arr[i].
if arr[i] > first:
third = second
second = first
first = arr[i]
# If arr[i] is greater than second,
# set third to second and second
# to arr[i].
elif arr[i] > second:
third = second
second = arr[i]
# If arr[i] is greater than third,
# set third to arr[i].
elif arr[i] > third:
third = arr[i]
# Return the third largest element
return third
if __name__ == "__main__":
arr = [1, 14, 2, 16, 10, 20]
print(thirdLargest(arr))
C#
// C# program to find the third largest
// element in an array.
using System;
class GfG {
static int thirdLargest(int[] arr) {
int n = arr.Length;
int first = int.MinValue, second = int.MinValue,
third = int.MinValue;
for (int i = 0; i < n; i++) {
// If arr[i] is greater than first,
// set third to second, second to
// first and first to arr[i].
if (arr[i] > first) {
third = second;
second = first;
first = arr[i];
}
// If arr[i] is greater than second,
// set third to second and second
// to arr[i].
else if (arr[i] > second) {
third = second;
second = arr[i];
}
// If arr[i] is greater than third,
// set third to arr[i].
else if (arr[i] > third) {
third = arr[i];
}
}
// Return the third largest element
return third;
}
static void Main() {
int[] arr = {1, 14, 2, 16, 10, 20};
Console.WriteLine(thirdLargest(arr));
}
}
JavaScript
// JavaScript program to find the third largest
// element in an array.
// Function to find the third largest element
function thirdLargest(arr) {
let n = arr.length;
let first = -Infinity, second = -Infinity,
third = -Infinity;
for (let i = 0; i < n; i++) {
// If arr[i] is greater than first,
// set third to second, second to
// first and first to arr[i].
if (arr[i] > first) {
third = second;
second = first;
first = arr[i];
}
// If arr[i] is greater than second,
// set third to second and second
// to arr[i].
else if (arr[i] > second) {
third = second;
second = arr[i];
}
// If arr[i] is greater than third,
// set third to arr[i].
else if (arr[i] > third) {
third = arr[i];
}
}
// Return the third largest element
return third;
}
let arr = [1, 14, 2, 16, 10, 20];
console.log(thirdLargest(arr));
Related Articles:
Similar Reads
Javascript Program for Third largest element in an array of distinct elements Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example : Input: arr[] = {1, 14, 2, 16, 10, 20}Output: The third Largest element is 14Explanation: Largest element is 20, second largest element is 16 and third largest element is 14Inp
6 min read
k-th distinct (or non-repeating) element among unique elements in an array. Given an integer array arr[], print kth distinct element in this array. The given array may contain duplicates and the output should print the k-th element among all unique elements. If k is more than the number of distinct elements, print -1.Examples:Input: arr[] = {1, 2, 1, 3, 4, 2}, k = 2Output:
7 min read
Print all Distinct (Unique) Elements in given Array Given an integer array arr[], print all distinct elements from this array. The given array may contain duplicates and the output should contain every element only once.Examples: Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45}Output: {12, 10, 9, 45, 2}Input: arr[] = {1, 2, 3, 4, 5}Output: {1, 2, 3, 4,
11 min read
Count distinct elements in an array in Python Given an unsorted array, count all distinct elements in it. Examples: Input : arr[] = {10, 20, 20, 10, 30, 10} Output : 3 Input : arr[] = {10, 20, 20, 10, 20} Output : 2 We have existing solution for this article. We can solve this problem in Python3 using Counter method. Approach#1: Using Set() Thi
2 min read
Print sorted distinct elements of array Given an array that might contain duplicates, print all distinct elements in sorted order. Examples: Input : 1, 3, 2, 2, 1Output : 1 2 3Input : 1, 1, 1, 2, 2, 3Output : 1 2 3The simple Solution is to sort the array first, then traverse the array and print only first occurrences of elements. Algorith
6 min read
Find the only different element in an array Given an array of integers where all elements are same except one element, find the only different element in the array. It may be assumed that the size of the array is at least two.Examples: Input : arr[] = {10, 10, 10, 20, 10, 10} Output : 3 arr[3] is the only different element.Input : arr[] = {30
10 min read
Count Distinct ( Unique ) elements in an array Given an array arr[] of length N, The task is to count all distinct elements in arr[]. Examples: Input: arr[] = {10, 20, 20, 10, 30, 10}Output: 3Explanation: There are three distinct elements 10, 20, and 30. Input: arr[] = {10, 20, 20, 10, 20}Output: 2 Naïve Approach: Create a count variable and ru
15 min read
Distinct adjacent elements in an array Given an array, find whether it is possible to obtain an array having distinct neighbouring elements by swapping two neighbouring array elements. Examples: Input : 1 1 2 Output : YES swap 1 (second last element) and 2 (last element), to obtain 1 2 1, which has distinct neighbouring elements . Input
7 min read
JavaScript Program to Print All Distinct Elements in an Integer Array Given an Array of Integers consisting of n elements with repeated elements, the task is to print all the distinct elements of the array using JavaScript. We can get the distinct elements by creating a set from the integer array. Examples: Input : arr = [ 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9 ] Outpu
3 min read
Construct an Array having K Subarrays with all distinct elements Given integers N and K, the task is to construct an array arr[] of size N using numbers in the range [1, N] such that it has K sub-arrays whose all the elements are distinct. Note: If there are multiple possible answers return any of them. Examples: Input: N = 5, K = 8Output: {1, 2, 3, 3, 3}Explanat
7 min read