How to Find Second Max Number In C#

Finding the second largest (or second maximum) number in a collection is a common problem in programming interviews and real-world applications. In this article, we'll explore how to find the second maximum number in an array and in a positive integer using C#.

How to Find Second Max Number In C#

In an Array

Finding the second largest number in an array is a classic programming interview question. While it may seem simple at first, the challenge lies in handling edge cases—like duplicate values or arrays with fewer than two elements. In this article, we'll explore how to solve this problem effectively using C#.

Idea: We use two variables:
  • first stores the largest number found so far.
  • second stores the second largest.
C# Code
 using System;  
 class Program  
 {  
   static int? FindSecondLargest(int[] arr)  
   {  
     if (arr.Length < 2)  
       return null;  
     int? first = null;  
     int? second = null;  
     foreach (int num in arr)  
     {  
       if (first == null || num > first)  
       {  
         second = first;  
         first = num;  
       }  
       else if ((num < first) && (second == null || num > second))  
       {  
         second = num;  
       }  
     }  
     return second;  
   }  
   static void Main()  
   {  
     int[] numbers = { 12, 35, 1, 10, 34, 1 };  
     var result = FindSecondLargest(numbers);  
     Console.WriteLine(result.HasValue ? $"Second largest: {result}" : "No second largest value found.");  
   }  
 }  

Output:
 Second largest: 34  

In an Intiger Number

Working with digits inside a number is a common problem in coding interviews and programming exercises. One useful task is finding the second largest digit within a given positive integer. To find the second maximum digit in a positive integer, you can follow this simple approach:

The following code example extracts all its digits from a given intiger value and return the second highest digit. If the number has less than two unique digits (like 1111 or 5), return a message indicating there's no second maximum digit.

C# Code
 using System;  
 namespace FindSecondLargest  
 {  
   internal class Program  
   {  
     static void Main(string[] args)  
     {  
       int number = 5378159;  
       var result = FindinPositiveIntiger(number);  
       if (result.HasValue)  
         Console.WriteLine("Second max digit: " + result.Value);  
       else  
         Console.WriteLine("No second max digit found.");  
       Console.ReadLine();  
     }  
     static int? FindinPositiveIntiger(int number)  
     {  
       if (number == 0)  
         return null;  
       if (number <= 9)  
         return null;  
       int? firstmax = null;  
       int? secondmax = null;  
       while (number > 0)  
       {  
         int num = number % 10;  
         if (firstmax == null || num > firstmax)  
         {  
           secondmax = firstmax;  
           firstmax = num;  
         }  
         else if (num < firstmax && (secondmax==null || num>secondmax))  
         {  
           secondmax = num;  
         }  
         number /= 10;  
       }  
       return secondmax;  
     }  
   }  
 }  

Output:
 Second max digit: 8  

Summary

Finding the second maximum number in C# can be done using sorting or a more efficient single-pass approach. The method you choose depends on the context—whether performance or code readability is more important. I hope you found this information helpful.

Thanks

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال