---
title: "How to find the second-largest integer in an array using a loop?"  
description: "How to find the second-largest integer in an array using a loop?"  
author: "Revati S Misra"  
published: 2023-02-12  
updated: 2023-06-15  
canonical: https://www.mindstick.com/forum/157393/how-to-find-the-second-largest-integer-in-an-array-using-a-loop  
category: "c#"  
tags: ["c#", "java", "programs"]  
reading_time: 2 minutes  

---

# How to find the second-largest integer in an array using a loop?

Find the [second largest](https://www.mindstick.com/forum/156802/write-a-program-that-take-n-integer-number-as-input-and-display-the-second-largest-value) number from the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server)'s [input](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java) of an unsorted [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net)

## Replies

### Reply by Aryan Kumar

Here is a Python code that finds the second-largest integer in an array using a loop:

Python

```plaintext
def second_largest(array):
    largest = array[0]
    second_largest = array[0]

    for i in range(1, len(array)):
        if array[i] > largest:
            second_largest = largest
            largest = array[i]
        elif array[i] > second_largest:
            second_largest = array[i]

    return second_largest

array = [1, 2, 3, 4, 5]
print(second_largest(array))
```

This code works by first finding the largest integer in the array. Then, it iterates through the array again, and checks if any of the other integers are larger than the second-largest integer. If so, it updates the second-largest integer to be the larger integer. Finally, the code returns the second-largest integer.

Here is an explanation of how the code works:

- The def keyword defines a function called second_largest.
- The array parameter is the array that we want to find the second-largest integer in.
- The largest variable stores the largest integer in the array.
- The second_largest variable stores the second-largest integer in the array.
- The for loop iterates through the array, starting at index 1.
- The if statement checks if the current integer is larger than the largest variable.
- If the current integer is larger than the largest variable, the second_largest variable is updated to be the largest variable, and the largest variable is updated to be the current integer.
- If the current integer is not larger than the largest variable, the second_largest variable is not updated.
- The return statement returns the second_largest variable.

I hope this helps!

### Reply by Ashutosh Patel

There is a following simple C# program to find the [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society)-largest number in a given unordered array,

```cs
using System;
namespace HelloWorld
{
 class Program
 {
   static void Main(string[] args)
   {
     int[] num={5,4,8,3,7,20,45,30,25};
     Program.FindSecondLargNumber(num);
   }
   static void FindSecondLargNumber(int[] arr)
   {

       int max1 = int.MinValue;
       int max2 = int.MinValue;
       foreach (int i in arr)
       {
           if (i > max1)
           {
               max2 = max1;
               max1 = i;
           }
           else if (i >= max2 && i != max1)
           {
               max2 = i;
           }
       }
       Console.WriteLine(max2); ;
   }
 }
}

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