VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-largest-three-elements-in-an-array/

⇱ Largest three distinct elements in an array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Largest three distinct elements in an array

Last Updated : 2 Feb, 2026

Given an array arr[], the task is to find the top three largest distinct integers present in the array.

Note: If there are less than three distinct elements in the array, then return the available distinct numbers in descending order.

Examples :

Input: arr[] = [10, 4, 3, 50, 23, 90]
Output: [90, 50, 23]

Input: arr[] = [10, 9, 9]
Output: [10, 9]
There are only two distinct elements

Input: arr[] = [10, 10, 10]
Output: [10]
There is only one distinct element

[Naive Approach] Three Traversals - O(n) Time and O(1) Space

First find the maximum in one pass, then find the maximum excluding it for the second largest, and again exclude both to get the third largest.


Output
34 13 12 

[Efficient Approach] One Traversal - O(n) Time and O(1) Space

Keep track of the three largest values while scanning the array once, updating their positions whenever a bigger element is found, so the largest, second largest, and third largest are always maintained in order.


Output
34 13 12 
Comment
Article Tags: