Tuesday, 21 June 2011

C#.Net : Find the Nth Largest Number in an Integer Array

A couple of days ago Namith From Microsoft Asked me to write down the code or tell the code on Phone for "Find the Nth Largest Number in an Integer Array". Not easy to tell on Mobile but more easy to write it in Visual Studio. It was an interesting question so took some time to really try it out.

Below is the code, I used Visual Studio 2008, C# Console Application


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace _5thLargest
{
    class Program
    {
        static void Main(string[] args)
        {
            //First Largest Number
            GetNthLargesNumber(1);


            //Second Largest Number
            GetNthLargesNumber(2);


            //Third Largest Number
            GetNthLargesNumber(3);


            //Fourth Largest Number
            GetNthLargesNumber(4);


            //5th Largest Number
            GetNthLargesNumber(5);


            Console.Read();
        }


        /// <summary>
        /// GetNthLargesNumber
        /// </summary>
        private static void GetNthLargesNumber(int NthLargestNumber)
        {
            int[] iNumSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 88, 76, 45, 44 };
            int maxValue = 0;


            if (NthLargestNumber > 0)
            {
                for (int k = 0; k < NthLargestNumber; k++)
                {
                    for (int i = 0; i < iNumSet.Length; i++)
                    {


                        if (i == 0)
                        {
                            maxValue = iNumSet[i];
                        }
                        else
                        {
                            if (maxValue < iNumSet[i])
                            {
                                maxValue = iNumSet[i];
                            }
                        }


                    }
                    int intRemoveThis = maxValue;
                    iNumSet = iNumSet.Where(val => val != intRemoveThis).ToArray();
                }


                PrintDetails(NthLargestNumber, maxValue);
              
            }


        }


        /// <summary>
        /// PrintDetails
        /// </summary>
        /// <param name="NthLargestNumber"></param>
        /// <param name="maxValue"></param>
        private static void PrintDetails(int NthLargestNumber, int maxValue)
        {
            switch (NthLargestNumber)
            {
                case 1:
                    Console.WriteLine("{0}st Largest Value in iNumSet Array of Elements is  = " + maxValue.ToString(), NthLargestNumber);
                    break;
                case 2:
                    Console.WriteLine("{0}nd Largest Value in iNumSet Array of Elements is  = " + maxValue.ToString(), NthLargestNumber);
                    break;
                case 3:
                    Console.WriteLine("{0}rd Largest Value in iNumSet Array of Elements is  = " + maxValue.ToString(), NthLargestNumber);
                    break;
                default:
                    Console.WriteLine("{0}th Largest Value in iNumSet Array of Elements is  = " + maxValue.ToString(), NthLargestNumber);


                    break;




            }
        }
    }
}






OutPut Should be as below






No comments:

Post a Comment