Tech Point Fundamentals

Thursday, January 20, 2022

C# Program to Find the Second Largest Element in an Array

C# Program to Find the Second Largest Element in an Array

coding-interview-question-csharp

Most of the IT companies check the coding skills and problem-solving skills as well along with the theoretical interview questions. Sometimes you are free to write the pseudo code and sometimes you are asked to write the complete program either on any paper or any editor. 


This question is asked in the coding interview to write the program. Here you can find the program as well as a live running program so that you can test the program immediately.


Watch our videos here





Question: 


How can you find the second largest element in an array using only a single loop? Write a program to find the second largest element in an array.









C# Program to Find the Second Largest Element in an Array


       
 

// Author 	: Tech Point Fundamentals
// Website 	: www.techpointfunda.com
// Channel	: https://www.youtube.com/c/TechPointFundamentals
// Demo		: Second Largest Array Element

using System;

public class Program
{
public static void Main()
{
  int[] unsortedArray = new int[8] {5, 3, 6, 2, 1, 4, 8, 7};			
  		
  Console.WriteLine("Input UnSorted Array: ");	
  Console.WriteLine("------------------------------------------------ ");	
  for( int i = 0; i < unsortedArray.Length; i++)
  {
  	Console.WriteLine(unsortedArray[i]);
  }
  
  Console.WriteLine("\n\nSecond Largest Element - Way1 : " + SecondLargetElement(unsortedArray));	
  Console.WriteLine("Second Largest Element - Way2 : " + SecondLargetElement2(unsortedArray));
  Console.WriteLine("Second Largest Element - Way3 : " + SecondLargetElement3(unsortedArray));
}	
	
static int SecondLargetElement(int[] array)
{		
	int large, small;
	
	if(array[0] > array[1]) 
	{
	  large = array[0];
	  small  = array[1];
	}
	else 
	{
	  large = array[1];
	  small = array[0];
	}
	 
   for(int i = 2; i < array.Length; i++) 
   {
	  if( large < array[i] ) 
	  {
		 small = large;
		 large = array[i];
	  } 
	  else if(small < array[i] ) 
	  {
		small =  array[i];
	  }
   }
	return small;  
}		
	
static int SecondLargetElement2(int[] array)
{		
	int large = 0, small = 0;	
 
	for(int i = 0; i < array.Length; i++) 
	{
		if (array[i] > large && small <= large)
		{
			small = large;
			large = array[i];
		}
		else if(array[i] > small)
		{
			small = array[i];               
		}  
	}
	return small;  
}		
	
// Using Foreach Loop
static int SecondLargetElement3(int[] array)
{		
	int large = 0, small = 0;	
 
	foreach (int i in array)  
	{  
		if (i > large)  
		{  
			small = large;  
			large = i;  
		}  
		else if (i >= small && i != large)  
		{  
			small = i;  
		}  
	}  
	return small;  
}		
}





Output: 



                Input UnSorted Array: ------------------------------------------------ 5 3 6 2 1 4 8 7 Second Largest Element - Way1 : 7 Second Largest Element - Way2 : 7 Second Largest Element - Way3 : 7


Live Demo






No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.