Find Third Largest Number From Array Using C#

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

  • Create three variables called, firstHighest, secondHighest, and thirdHighest, to store indices of the three highest elements of the array.
  • Traverse the input array from start to the end.
  • For every index check if the element is higher than the first or not. Update the value of first, if the element is higher, and assign the value of first to second and second to third.
  • So the highest element gets updated and the elements previously stored as largest become second highest, and the second highest element becomes third highest.
  • Else if the element is larger than the second, then update the value of the second, and the second largest element becomes the third highest.
  • If the element is higher than the third, then update the third.
  • Print the value of the thirdHighest after traversing the array.

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.