Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Third Maximum Number problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Third Maximum Number problem solution, we have given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.

Leetcode Third Maximum Number problem solution

Leetcode Third Maximum Number problem solution in Python.

class Solution:
    def thirdMax(self, nums: List[int]) -> int:
        d=len(set(nums))
        x=sorted(set(nums))
        if d==2:
            return x[-1]
        elif d==1:
            return nums[0]
        else:
            return x[-3]

Third Maximum Number problem solution in Java.

public int thirdMax(int[] nums) 
    {
        int keepMax = nums[0];
        int thirdMax = keepMax;
        
        long max = Long.MAX_VALUE;
        for (int i = 0; i < 3; i++)
        {
            long tmp = Long.MIN_VALUE;
            for (int j = 0; j < nums.length; j++)
            {
                if (nums[j] < max && nums[j] > tmp) tmp = nums[j];
            }
            if (i == 0) { keepMax = (int)tmp; max = tmp; }

            if (i == 1) { max = tmp;}

            if (i == 2)
            {   
                if (tmp == Long.MIN_VALUE) return keepMax;
                else thirdMax = (int)tmp;
            }
        }
        
        return thirdMax;
    }

Problem solution in C++.

class Solution {
public:
    int thirdMax(vector<int>& nums) {
        set<int>S(nums.begin(),nums.end());
        priority_queue<int> pq(S.begin(),S.end());
        if(pq.size() == 1 || pq.size() == 2) return pq.top();
        pq.pop();
        pq.pop();
        return pq.top();
    }
 };

Problem solution in C.

int j=0;
	int length=0;
	int m=0;
   int* r= malloc (sizeof(int)*numsSize) ;
for(int i=0;i<numsSize-1;i++)
{
	for( j=0;j<numsSize-1-i;j++)
	{
		if(nums[j]<nums[j+1])
		{
			int temp = nums[j];
			nums[j]=nums[j+1];
			nums[j+1]=temp;
		}
	}
}



for (int i=0; i<numsSize; i++)
{
for (j=0; j<i; j++)
{
if (nums[i] == nums[j])
break;
}
if (i == j)
{
	r[m++]=nums[i];
	length++;
		}

}

if(length>=3)
    return r[2];
else
    return r[0];

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes