Home / Programs / Write a program that finds the second largest element in a given list of N numbers.
🚀 Programming Example

Write a program that finds the second largest element in a given list of N numbers.

👁 567 Views
💻 Practical Program
📘 Step Learning

The most basic solution for this problem is that if there are two elements in the list then the smaller of the two will be the second largest. In this case, we would set the second largest number to ‘–9999’, a value not possible in the list.

In given problem, the list will be searched linearly for the required second largest number. Two variables called firstLarge and secLarge would be employed to store the first and second largest numbers. The following algorithm will be used:

💻 Program Code

/* This program finds the second largest in a given list of numbers */

#include <stdio.h>

int Num, firstLarge, secLarge;

int N, i;

void main()

{

printf ("\n Enter the size of the list");

scanf ("%d", & N);

printf ("\n Enter the list one by one");

scanf ("%d", &Num);

firstLarge = Num;

secLarge = - 9999;

for (i = 2; i <= N; i++)

{

  scanf ("%d", &Num);

  if (firstLarge < Num)

  {secLarge = firstLarge;

  firstLarge = Num;

  }

  else

    if (secLarge < Num)

    secLarge = Num;

}

printf ("\n Second Large = %d", secLarge);

}
                        

🖥 Program Output

Enter the size of the list: 5                                                                                                     
                                                                                                                                 
 Enter the list one by one
1                                                                                                      
2                                                                                                                                
3                                                                                                                                
4                                                                                                                                
5                                                                                                                                
                                                                                
                            

📘 Explanation

The above discussed search through a list, stored in an array, has the following characteristics:

  • The search is linear.
  • The search starts from the first element and continues in a sequential fashion from element to element till the desired entry is found.
  • In the worst case, a total number of N steps need to be taken for a list of size N.

Thus, the linear search is slow and to some extent inefficient. In special circumstances, faster searches can be applied.

For instance, binary search is a faster method as compared to linear search. It mimics the process of searching a name in a directory wherein one opens a page in the middle of the directory and examines the page for the required name. If it is found, the search stops; otherwise, the search is applied either to first half of the directory or to the second half.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.