Find Second largest element in an array

Here given code implementation process.

// C Program 
// Find Second largest element in an array
#include <stdio.h>
#include <limits.h>

//Display elements of given array
void printArray(int arr[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("  %d", arr[i]);
	}
	printf("\n");
}
// Finding the second largest element of given collection 
void secondLargest(int arr[], int size)
{
	if (size <= 1)
	{
		return;
	}
	else
	{
		// Define useful resultant variables
		int first = INT_MIN;
		int second = INT_MIN;
		int status = 0;
		// Execute loop (0...size)
		for (int i = 0; i < size; i++)
		{
			// Compare the array element values
			if (first < arr[i])
			{
				if (second < first)
				{
					status = 1;
					second = first;
				}
				first = arr[i];
			}
			else if (arr[i] != first && second < arr[i])
			{
				// When get a new second highest element
				status = 1;
				second = arr[i];
			}
		}
      	// Display array elements
      	printArray(arr,size);
		if (status == 0)
		{
			printf(" No second Largest element in this array \n");
		}
		else
		{
			// Display result
			printf("  Second Largest  : %d\n", second);
		}
	}
}
int main()
{
	//Define collection of array elements
	int arr[] = 
    {
		1 , 4 , 9 , 5 , 7 , 9 , 4 , 2 , 4 , 9 , 2
	};
	//Get the size of array
	int size = sizeof(arr) / sizeof(arr[0]);
	secondLargest(arr, size);
	return 0;
}

Output

  1  4  9  5  7  9  4  2  4  9  2
  Second Largest  : 7
/*
    Java Program
    Find Second largest element in an array
*/
public class SearchElement
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("   " + arr[i]);
		}
		System.out.print("\n");
	}
	// Finding the second largest element of given collection 
	public void secondLargest(int[] arr, int size)
	{
		if (size <= 1)
		{
			return;
		}
		else
		{
			// Define useful resultant variables
			int first  = Integer.MIN_VALUE;
			int second = Integer.MIN_VALUE;
			boolean status = false;
			// Execute loop (0...size)
			for (int i = 0; i < size; i++)
			{
				// Compare the array element values
				if (first < arr[i])
				{
					if (second < first)
					{
						status = true;
						second = first;
					}
					first = arr[i];
				}
				else if (arr[i] != first && second < arr[i])
				{
					// When get a new second highest element
					status = true;
					second = arr[i];
				}
			}
			// Display array elements
			printArray(arr, size);
			if (status == false)
			{
				System.out.print(" No second Largest element in this array \n");
			}
			else
			{
				// Display result
				System.out.print(" Second Largest : " + second + "\n");
			}
		}
	}
	public static void main(String[] args)
	{
		SearchElement task = new SearchElement();
		//Define collection of array elements
		int[] arr = 
        {
			1 , 4 , 9 , 5 , 7 , 9 , 4 , 2 , 4 , 9 , 2
		};
		//Get the size of array
		int size = arr.length;
		task.secondLargest(arr, size);
	}
}

Output

   1   4   9   5   7   9   4   2   4   9   2
 Second Largest : 7
// Include header file
#include <iostream>
#include <limits.h>
using namespace std;

/*
    C++ Program
    Find Second largest element in an array
*/

class SearchElement
{
	public:
		//Display elements of given array
		void printArray(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "   " << arr[i];
			}
			cout << "\n";
		}
	// Finding the second largest element of given collection
	void secondLargest(int arr[], int size)
	{
		if (size <= 1)
		{
			return;
		}
		else
		{
			// Define useful resultant variables
			int first = INT_MIN;
			int second = INT_MIN;
			bool status = false;
			// Execute loop (0...size)
			for (int i = 0; i < size; i++)
			{
				// Compare the array element values
				if (first < arr[i])
				{
					if (second < first)
					{
						status = true;
						second = first;
					}
					first = arr[i];
				}
				else if (arr[i] != first && second < arr[i])
				{
					// When get a new second highest element
					status = true;
					second = arr[i];
				}
			}
			// Display array elements
			this->printArray(arr, size);
			if (status == false)
			{
				cout << " No second Largest element in this array \n";
			}
			else
			{
				// Display result
				cout << " Second Largest : " << second << "\n";
			}
		}
	}
};
int main()
{
	SearchElement task = SearchElement();
	//Define collection of array elements
	int arr[] = {
		1 , 4 , 9 , 5 , 7 , 9 , 4 , 2 , 4 , 9 , 2
	};
	//Get the size of array
	int size = sizeof(arr) / sizeof(arr[0]);
	task.secondLargest(arr, size);
	return 0;
}

Output

   1   4   9   5   7   9   4   2   4   9   2
 Second Largest : 7
// Include namespace system
using System;
/*
    C# Program
    Find Second largest element in an array
*/
public class SearchElement
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("   " + arr[i]);
		}
		Console.Write("\n");
	}
	// Finding the second largest element of given collection
	public void secondLargest(int[] arr, int size)
	{
		if (size <= 1)
		{
			return;
		}
		else
		{
			// Define useful resultant variables
			int first = int.MinValue;
			int second = int.MinValue;
			Boolean status = false;
			// Execute loop (0...size)
			for (int i = 0; i < size; i++)
			{
				// Compare the array element values
				if (first < arr[i])
				{
					if (second < first)
					{
						status = true;
						second = first;
					}
					first = arr[i];
				}
				else if (arr[i] != first && second < arr[i])
				{
					// When get a new second highest element
					status = true;
					second = arr[i];
				}
			}
			// Display array elements
			printArray(arr, size);
			if (status == false)
			{
				Console.Write(" No second Largest element in this array \n");
			}
			else
			{
				// Display result
				Console.Write(" Second Largest : " + second + "\n");
			}
		}
	}
	public static void Main(String[] args)
	{
		SearchElement task = new SearchElement();
		//Define collection of array elements
		int[] arr = {
			1 , 4 , 9 , 5 , 7 , 9 , 4 , 2 , 4 , 9 , 2
		};
		//Get the size of array
		int size = arr.Length;
		task.secondLargest(arr, size);
	}
}

Output

   1   4   9   5   7   9   4   2   4   9   2
 Second Largest : 7
<?php
/*
    Php Program
    Find Second largest element in an array
*/
class SearchElement
{
	//Display elements of given array
	public	function printArray( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "   ". $arr[$i];
		}
		echo "\n";
	}
	// Finding the second largest element of given collection
	public	function secondLargest( & $arr, $size)
	{
		if ($size <= 1)
		{
			return;
		}
		else
		{
			// Define useful resultant variables
			$first = -PHP_INT_MAX;
			$second = -PHP_INT_MAX;
			$status = false;
			// Execute loop (0...size)
			for ($i = 0; $i < $size; $i++)
			{
				// Compare the array element values
				if ($first < $arr[$i])
				{
					if ($second < $first)
					{
						$status = true;
						$second = $first;
					}
					$first = $arr[$i];
				}
				else if ($arr[$i] != $first && $second < $arr[$i])
				{
					// When get a new second highest element
					$status = true;
					$second = $arr[$i];
				}
			}
			// Display array elements
			$this->printArray($arr, $size);
			if ($status == false)
			{
				echo " No second Largest element in this array \n";
			}
			else
			{
				// Display result
				echo " Second Largest : ". $second ."\n";
			}
		}
	}
}

function main()
{
	$task = new SearchElement();
	//Define collection of array elements
	$arr = array(1, 4, 9, 5, 7, 9, 4, 2, 4, 9, 2);
	//Get the size of array
	$size = count($arr);
	$task->secondLargest($arr, $size);
}
main();

Output

   1   4   9   5   7   9   4   2   4   9   2
 Second Largest : 7
/*
    Node Js Program
    Find Second largest element in an array
*/
class SearchElement
{
	//Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("   " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Finding the second largest element of given collection
	secondLargest(arr, size)
	{
		if (size <= 1)
		{
			return;
		}
		else
		{
			// Define useful resultant variables
			var first = -Number.MAX_VALUE;
			var second = -Number.MAX_VALUE;
			var status = false;
			// Execute loop (0...size)
			for (var i = 0; i < size; i++)
			{
				// Compare the array element values
				if (first < arr[i])
				{
					if (second < first)
					{
						status = true;
						second = first;
					}
					first = arr[i];
				}
				else if (arr[i] != first && second < arr[i])
				{
					// When get a new second highest element
					status = true;
					second = arr[i];
				}
			}
			// Display array elements
			this.printArray(arr, size);
			if (status == false)
			{
				process.stdout.write(" No second Largest element in this array \n");
			}
			else
			{
				// Display result
				process.stdout.write(" Second Largest : " + second + "\n");
			}
		}
	}
}

function main()
{
	var task = new SearchElement();
	//Define collection of array elements
	var arr = [1, 4, 9, 5, 7, 9, 4, 2, 4, 9, 2];
	//Get the size of array
	var size = arr.length;
	task.secondLargest(arr, size);
}
main();

Output

   1   4   9   5   7   9   4   2   4   9   2
 Second Largest : 7
import sys

#  Python 3 Program
#  Find Second largest element in an array

class SearchElement :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print("   ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Finding the second largest element of given collection
	def secondLargest(self, arr, size) :
		if (size <= 1) :
			return
		else :
			#  Define useful resultant variables
			first = -sys.maxsize
			second = -sys.maxsize
			status = False
			#  Execute loop (0...size)
			i = 0
			while (i < size) :
				#  Compare the array element values
				if (first < arr[i]) :
					if (second < first) :
						status = True
						second = first
					
					first = arr[i]
				
				elif(arr[i] != first and second < arr[i]) :
					#  When get a new second highest element
					status = True
					second = arr[i]
				
				i += 1
			
			#  Display array elements
			self.printArray(arr, size)
			if (status == False) :
				print(" No second Largest element in this array ")
			else :
				#  Display result
				print(" Second Largest : ", second )
			
		
	

def main() :
	task = SearchElement()
	# Define collection of array elements
	arr = [1, 4, 9, 5, 7, 9, 4, 2, 4, 9, 2]
	# Get the size of array
	size = len(arr)
	task.secondLargest(arr, size)

if __name__ == "__main__": main()

Output

    1    4    9    5    7    9    4    2    4    9    2
 Second Largest :  7
#  Ruby Program
#  Find Second largest element in an array

class SearchElement 
	# Display elements of given array
	def printArray(arr, size) 
		i = 0
		while (i < size) 
			print("   ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Finding the second largest element of given collection
	def secondLargest(arr, size) 
		if (size <= 1) 
			return
		else 
			#  Define useful resultant variables
			first = -(2 ** (0. size * 8 - 2))
			second = -(2 ** (0. size * 8 - 2))
			status = false
			#  Execute loop (0...size)
			i = 0
			while (i < size) 
				#  Compare the array element values
				if (first < arr[i]) 
					if (second < first) 
						status = true
						second = first
					end

					first = arr[i]
				elsif(arr[i] != first && second < arr[i]) 
					#  When get a new second highest element
					status = true
					second = arr[i]
				end

				i += 1
			end

			#  Display array elements
			self.printArray(arr, size)
			if (status == false) 
				print(" No second Largest element in this array \n")
			else 
				#  Display result
				print(" Second Largest : ", second ,"\n")
			end

		end

	end

end

def main() 
	task = SearchElement.new()
	# Define collection of array elements
	arr = [1, 4, 9, 5, 7, 9, 4, 2, 4, 9, 2]
	# Get the size of array
	size = arr.length
	task.secondLargest(arr, size)
end

main()

Output

   1   4   9   5   7   9   4   2   4   9   2
 Second Largest : 7
/*
    Scala Program
    Find Second largest element in an array
*/
class SearchElement
{
	//Display elements of given array
	def printArray(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("   " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Finding the second largest element of given collection
	def secondLargest(arr: Array[Int], size: Int): Unit = {
		if (size <= 1)
		{
			return;
		}
		else
		{
			// Define useful resultant variables
			var first: Int = Int.MinValue;
			var second: Int = Int.MinValue;
			var status: Boolean = false;
			var i: Int = 0;
			// Execute loop (0...size)
			while (i < size)
			{
				// Compare the array element values
				if (first < arr(i))
				{
					if (second < first)
					{
						status = true;
						second = first;
					}
					first = arr(i);
				}
				else if (arr(i) != first && second < arr(i))
				{
					// When get a new second highest element
					status = true;
					second = arr(i);
				}
				i += 1;
			}
			// Display array elements
			this.printArray(arr, size);
			if (status == false)
			{
				print(" No second Largest element in this array \n");
			}
			else
			{
				// Display result
				print(" Second Largest : " + second + "\n");
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SearchElement = new SearchElement();
		//Define collection of array elements
		var arr: Array[Int] = Array(1, 4, 9, 5, 7, 9, 4, 2, 4, 9, 2);
		//Get the size of array
		var size: Int = arr.length;
		task.secondLargest(arr, size);
	}
}

Output

   1   4   9   5   7   9   4   2   4   9   2
 Second Largest : 7
/*
    Swift 4 Program
    Find Second largest element in an array
*/
class SearchElement
{
	//Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("   ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Finding the second largest element of given collection
	func secondLargest(_ arr: [Int], _ size: Int)
	{
		if (size <= 1)
		{
			return;
		}
		else
		{
			// Define useful resultant variables
			var first: Int = Int.min;
			var second: Int = Int.min;
			var status: Bool = false;
			var i: Int = 0;
			// Execute loop (0...size)
			while (i < size)
			{
				// Compare the array element values
				if (first < arr[i])
				{
					if (second < first)
					{
						status = true;
						second = first;
					}
					first = arr[i];
				}
				else if (arr[i] != first && second < arr[i])
				{
					// When get a new second highest element
					status = true;
					second = arr[i];
				}
				i += 1;
			}
			// Display array elements
			self.printArray(arr, size);
			if (status == false)
			{
				print(" No second Largest element in this array ");
			}
			else
			{
				// Display result
				print(" Second Largest : ", second );
			}
		}
	}
}
func main()
{
	let task: SearchElement = SearchElement();
	//Define collection of array elements
	let arr: [Int] = [1, 4, 9, 5, 7, 9, 4, 2, 4, 9, 2];
	//Get the size of array
	let size: Int = arr.count;
	task.secondLargest(arr, size);
}
main();

Output

    1    4    9    5    7    9    4    2    4    9    2
 Second Largest :  7
/*
    Kotlin Program
    Find Second largest element in an array
*/
class SearchElement
{
	//Display elements of given array
	fun printArray(arr: Array<Int>, size: Int): Unit
	{
		var i: Int = 0;
		while (i<size)
		{
			print("   " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Finding the second largest element of given collection
	fun secondLargest(arr: Array<Int>, size: Int): Unit
	{
		if (size <= 1)
		{
			return;
		}
		else
		{
			// Define useful resultant variables
			var first: Int = Int.MIN_VALUE;
			var second: Int = Int.MIN_VALUE;
			var status: Boolean = false;
			var i: Int = 0;
			// Execute loop (0...size)
			while (i<size)
			{
				// Compare the array element values
				if (first<arr[i])
				{
					if (second<first)
					{
						status = true;
						second = first;
					}
					first = arr[i];
				}
				else if (arr[i] != first && second<arr[i])
				{
					// When get a new second highest element
					status = true;
					second = arr[i];
				}
				i += 1;
			}
			// Display array elements
			this.printArray(arr, size);
			if (status == false)
			{
				print(" No second Largest element in this array \n");
			}
			else
			{
				// Display result
				print(" Second Largest : " + second + "\n");
			}
		}
	}
}
fun main(args: Array<String>): Unit
{
	var task: SearchElement = SearchElement();
	// Define collection of array elements
	var arr: Array<Int> = arrayOf(1, 4, 9, 5, 7, 9, 4, 2, 4, 9, 2);
	// Get the size of array
	var size: Int = arr.count();
	task.secondLargest(arr, size);
}

Output

   1   4   9   5   7   9   4   2   4   9   2
 Second Largest : 7


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved