We will give an array input and find the third largest and print it.

Input Format

You will be taking a number as an input which tells about the length of the array. On another line, array elements should be there with a single space between them.

We will find the third largest element in the array in a single traversal.

Input

7
25 26 7 8 10 11 79

Output Should be,

25

There can be several approaches to achieve the same result but we will try the most effective approach to achieve it.

Steps to get the third highest number

In the above approach, we can get the result with only single array traversing.

Please review the below code with single array traversing,

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class ThirdLargestElementCode {
    static void Main(String[] args) {
        Console.Write("Please enter array size: ");
        int arraySize = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine();
        Console.Write("Please enter array element by space seprated: ");
        string arrayElement = Console.ReadLine();
        if (!string.IsNullOrEmpty(arrayElement)) {
            var arr = arrayElement.Split(' ')?.Select(Int32.Parse)?.ToArray();
            /* Check arr length is equal to input size */
            if (arr.Length != arraySize) {
                Console.WriteLine("Array length mismatch.");
                return;
            }
            /* Check validation at least three elements */
            if (arr.Length < 3) {
                Console.Write(" Invalid Input ");
                return;
            }
            findThirdLargestElement(arr);
        } else {
            Console.WriteLine("Please enter valid input.");
        }
        Console.ReadLine();
    }
    static void findThirdLargestElement(int[] array) {
        // Initialize the firstHighest, secondHighest, and thirdHighest variables with a minimum value
        int firstHighest = array[0], secondHighest = int.MinValue, thirdHighest = int.MinValue;
        // Traverse array elements to find the third Largest
        for (int i = 1; i < array.Length; i++) {
            // If the current element is greater than the firstHighest, then update the all variables
            if (array[i] > firstHighest) {
                thirdHighest = secondHighest;
                secondHighest = firstHighest;
                firstHighest = array[i];
            }
            // If array[i] is in between firstHighest and secondHighest
            else if (array[i] > secondHighest) {
                thirdHighest = secondHighest;
                secondHighest = array[i];
            }
            // If array[i] is in between secondHighest and thirdHighest
            else if (array[i] > thirdHighest) {
                thirdHighest = array[i];
            }
        }
        Console.WriteLine();
        Console.Write("Third highest element: ");
        Console.Write(thirdHighest);
    }
}

Output

Third Largest Number From Array c#

Share your reviews and feedback if we can improve more.