---
title: "How to find the third largest number in an array using a loop?"  
description: "How to find the third largest number in an array using a loop?"  
author: "Revati S Misra"  
published: 2023-02-01  
updated: 2023-06-15  
canonical: https://www.mindstick.com/forum/157366/how-to-find-the-third-largest-number-in-an-array-using-a-loop  
category: "c#"  
tags: ["c#", "java", "programming language"]  
reading_time: 3 minutes  

---

# How to find the third largest number in an array using a loop?

How to find the [third largest](https://yourviews.mindstick.com/view/85530/will-india-be-the-third-largest-economy-by-2027) number from an unsorted [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer) [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) which enter by user.

## Replies

### Reply by Aryan Kumar

Here is a Java program to find the third largest number in an array using a loop:

Code snippet

```plaintext
import java.util.*;

public class ThirdLargestInArray {

    public static void main(String[] args) {

        // Create an array
        int[] arr = {12, 13, 1, 10, 34, 11};

        // Initialize the three largest elements as minus infinite
        int first = Integer.MIN_VALUE;
        int second = Integer.MIN_VALUE;
        int third = Integer.MIN_VALUE;

        // Iterate through the array
        for (int i = 0; i < arr.length; i++) {

            // If the current element is greater than the first largest element
            if (arr[i] > first) {

                // Update the third largest element
                third = second;

                // Update the second largest element
                second = first;

                // Update the first largest element
                first = arr[i];
            } else if (arr[i] > second && arr[i] != first) {

                // Update the third largest element
                third = second;

                // Update the second largest element
                second = arr[i];
            } else if (arr[i] > third && arr[i] != second) {

                // Update the third largest element
                third = arr[i];
            }
        }

        // Print the third largest element
        System.out.println("The third largest element is " + third);
    }
}
```

This program will print the following output:

Code snippet

```plaintext
The third largest element is 11
```

### Reply by Rahul Roi

```cs
// Online C# Editor for free
// Write, Edit and Run your C# code using C# Online Compiler

using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        int[] num={2,1,3,7,6,8,4,9};
        Console.WriteLine (FindLargetValue(num));
        //Console.WriteLine (FindLargetValue(num, -2));
        Console.WriteLine (FindLargetValue(num, 3));
        Console.WriteLine (FindLargetValue(num, 1));
    }

    public static int FindLargetValue(int[] array , int position=0)
    {
        Array.Sort(array);
        Array.Reverse(array);
        if(position<0 || position> array.Length)
        {
            throw new Exception('Index not found');
        }
        return position==0 ? array[position] :  array[position-1];
    }
}
```

If we put index in negative value then throw an exception **“Index Not Found”,** like -1, -0, -2

If you put position value higher than length of array then throw an exception **“Index Not Found”** .

## Output is →

## 9

**7**\
**8**\

### Reply by Tehran Noorani

This is the simple C# program to find the [third](https://yourviews.mindstick.com/story/4957/the-significance-of-the-third-eye-in-hinduism-more-than-just-a-symbol) largest number from a given integer array.

```cs
using System;
namespace Test
{
 class ThirdLargeNumber
 {
   static void Main(string[] args)
   {
   int[] num={2,1,3,7,6,8,4,9};
    LargestThirdNumber(num);
   }
     internal static void LargestThirdNumber(int[] arr)
     {
         int num1 = int.MinValue;
         int num2 = int.MinValue;
         int num3 = int.MinValue;
         foreach (int i in arr)
         {
             if (i > num1)
             {
                 num3 = num2;
                 num2 = num1;
                 num1 = i;
             }
             else if (i > num2 && i != num1)
             {
                 num3 = num2;
                 num2 = i;
             }
             else if (i > num3 && i != num2 && i != num1)
             {
                 num3 = i;
             }
         }
         Console.WriteLine(num3); ;
     }
 }
}

Output: 7
```


---

Original Source: https://www.mindstick.com/forum/157366/how-to-find-the-third-largest-number-in-an-array-using-a-loop

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
