Back-2-Basics : Largest and Second Largest in an Array(C#)

It’s a basics code, just sharing it since since will be useful for some body, who is in needy.

I am just refresing my OOPS and Basic knowledge, just experimenting. Ha ha ha.

Advice to guyz, don’t simply copy it, try to understand the logic and do it by yourself.

C#69 lines
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleAppPro
{
    /// <summary>
    ///
    /// </summary>
    public class NumberOps
    {
        /// <summary>
        /// Runs the number ops.
        /// </summary>
        public void RunNumberOps()
        {
            Random rand = new Random();
            int[] input = new int[20];
            Console.WriteLine("Numbers to Process");
            Console.WriteLine("----------------------------");
            for (int i = 0; i < 20; i++)
            {
                input[i] = rand.Next(100); //Generating numbers in random (below 100)
               Console.Write(String.Format("{0},", input[i]));
            }
            Console.WriteLine("nnRESULTn----------------------------");
            FirstNSecondLargest(input);
            Console.Read();
        }
        /// <summary>
        /// Firsts the N second largest.
        /// </summary>
        /// <param name="inVals">The in vals.</param>
        public void FirstNSecondLargest(int[] inVals)
        {
            int largest = 0;
            int smallest = 0;
            int secondLargest = 0;
            int secondSmallest = 0;
            largest  =  inVals[0];
            secondLargest = 0;
            secondSmallest = 0;
            smallest = inVals[0];
            for (int iIndex = 0; iIndex < inVals.Length; iIndex++)
            {
                if (inVals[iIndex] > largest) //Largest Value
                {
                    secondLargest = largest;
                    largest = inVals[iIndex];
                }
                else if (inVals[iIndex] >= secondLargest && inVals[iIndex] != largest)
                {
                    secondLargest = inVals[iIndex]; //second largest
                }
                if (smallest >  inVals[iIndex]) //Smallest Value
                {
                    secondSmallest = smallest;
                    smallest = inVals[iIndex];
                }
                else if (inVals[iIndex] < secondSmallest && inVals[iIndex] != smallest)
                {
                    secondSmallest = inVals[iIndex]; //second smallest
                }
            }
            Console.WriteLine(String.Format("Largest: {0}, Second Largest: {1}, Second Smallest: {3}, Smallest: {2}", largest, secondLargest, smallest, secondSmallest));
        }
    }
}

Smallest picture

PS: I have tested this only few times. If you find any issues in the logic. please notify me, so that i can fix it up for later use.

This is just an information sharing, hope this helps some body. Tweak it, if could and Happy Coding!!!


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Written by

Nithin Mohan TK

Hello Namaste! 👋🏻 I'm an accomplished Enterprise Solution Architect & Engineering Leader with 20+ years of progressive experience architecting, building, and scaling transformative enterprise solutions. ⚜️ Solution Architect / AI & Cloud 📌 Galway, Ireland

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.