# Find the largest and second largest element in an array

Here given code implementation process.

``````//C Program
//Find the largest and second largest element in an array
#include <stdio.h>

#include <limits.h>

void find_largest(int arr[], int size)
{
if (size <= 1)
{
return;
}
else
{
//Variable which is used to store the result
int first = INT_MIN;
int second = INT_MIN;
for (int i = 0; i < size; i++)
{
//compare the array element values
if (first < arr[i])
{
if (second < first)
{
second = first;
}
first = arr[i];
}
else if (second < arr[i])
{
second = arr[i];
}
}
//Display result
printf("First Largest : %d\n", first);
printf("Second Largest  : %d\n", second);
}
}
int main()
{
//Define collection of array elements
int arr[] = {
10,
3,
23,
86,
8,
9,
44,
5,
7
};
//Get the size of array
int size = sizeof(arr) / sizeof(arr);
find_largest(arr, size);
return 0;
}``````

#### Output

``````First Largest : 86
Second Largest  : 44``````
``````/*
Java Program
Find the largest and second largest element in an array
*/
class MyArray
{
public void find_largest(int[] arr, int size)
{
if (size <= 1)
{
return;
}
else
{
// Variable which is used to store the result
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
for (int i = 0; i < size; i++)
{
//compare the array element values
if (first < arr[i])
{
if (second < first)
{
second = first;
}
first = arr[i];
}
else if (second < arr[i])
{
second = arr[i];
}
}
//Display result
System.out.print("First Largest : " + first + "\n");
System.out.print("Second Largest  : " + second + "\n");
}
}
public static void main(String[] args)
{
MyArray obj = new MyArray();
//Define collection of array elements
int[] arr = {
10,
3,
23,
86,
8,
9,
44,
5,
7
};
//Get the size of array
int size = arr.length;
obj.find_largest(arr, size);
}
}``````

#### Output

``````First Largest : 86
Second Largest  : 44``````
``````//Include header file
#include <iostream>

#include<limits.h>

using namespace std;
/*
C++ Program
Find the largest and second largest element in an array
*/
class MyArray
{
public: void find_largest(int arr[], int size)
{
if (size <= 1)
{
return;
}
else
{
// Variable which is used to store the result
int first = INT_MIN;
int second = INT_MIN;
for (int i = 0; i < size; i++)
{
//compare the array element values
if (first < arr[i])
{
if (second < first)
{
second = first;
}
first = arr[i];
}
else if (second < arr[i])
{
second = arr[i];
}
}
//Display result
cout << "First Largest : " << first << "\n";
cout << "Second Largest  : " << second << "\n";
}
}
};
int main()
{
MyArray obj = MyArray();
int arr[] = {
10 , 3 , 23 , 86 , 8 , 9 , 44 , 5 , 7
};
//Get the size of array
int size = sizeof(arr) / sizeof(arr);
obj.find_largest(arr, size);
return 0;
}``````

#### Output

``````First Largest : 86
Second Largest  : 44``````
``````//Include namespace system
using System;
/*
C# Program
Find the largest and second largest element in an array
*/
public class MyArray
{
public void find_largest(int[] arr, int size)
{
if (size <= 1)
{
return;
}
else
{
// Variable which is used to store the result
int first = int.MinValue;
int second = int.MinValue;
for (int i = 0; i < size; i++)
{
//compare the array element values
if (first < arr[i])
{
if (second < first)
{
second = first;
}
first = arr[i];
}
else if (second < arr[i])
{
second = arr[i];
}
}
//Display result
Console.Write("First Largest : " + first + "\n");
Console.Write("Second Largest  : " + second + "\n");
}
}
public static void Main(String[] args)
{
MyArray obj = new MyArray();
int[] arr = {
10 , 3 , 23 , 86 , 8 , 9 , 44 , 5 , 7
};
//Get the size of array
int size = arr.Length;
obj.find_largest(arr, size);
}
}``````

#### Output

``````First Largest : 86
Second Largest  : 44``````
``````<?php
/*
Php Program
Find the largest and second largest element in an array
*/
class MyArray
{
public	function find_largest( & \$arr, \$size)
{
if (\$size <= 1)
{
return;
}
else
{
// Variable which is used to store the result
\$first = -PHP_INT_MAX;
\$second = -PHP_INT_MAX;
for (\$i = 0; \$i < \$size; \$i++)
{
//compare the array element values
if (\$first < \$arr[\$i])
{
if (\$second < \$first)
{
\$second = \$first;
}
\$first = \$arr[\$i];
}
else if (\$second < \$arr[\$i])
{
\$second = \$arr[\$i];
}
}
echo "First Largest : ". \$first ."\n";
echo "Second Largest  : ". \$second ."\n";
}
}
}

function main()
{
\$obj = new MyArray();
//Define collection of array elements
\$arr = array(10, 3, 23, 86, 8, 9, 44, 5, 7);
//Get the size of array
\$size = count(\$arr);
\$obj->find_largest(\$arr, \$size);
}
main();``````

#### Output

``````First Largest : 86
Second Largest  : 44``````
``````/*
Node Js Program
Find the largest and second largest element in an array
*/
class MyArray
{
find_largest(arr, size)
{
if (size <= 1)
{
return;
}
else
{
// Variable which is used to store the result
var first = -Number.MAX_VALUE;
var second = -Number.MAX_VALUE;
for (var i = 0; i < size; i++)
{
//compare the array element values
if (first < arr[i])
{
if (second < first)
{
second = first;
}
first = arr[i];
}
else if (second < arr[i])
{
second = arr[i];
}
}
process.stdout.write("First Largest : " + first + "\n");
process.stdout.write("Second Largest  : " + second + "\n");
}
}
}

function main()
{
var obj = new MyArray();
//Define collection of array elements
var arr = [10, 3, 23, 86, 8, 9, 44, 5, 7];
//Get the size of array
var size = arr.length;
obj.find_largest(arr, size);
}
main();``````

#### Output

``````First Largest : 86
Second Largest  : 44``````
``````import sys
#
#   Python 3 Program
#   Find the largest and second largest element in an array

class MyArray :
# Find 2 largest element in array
def find_largest(self, arr, size) :
if (size <= 1) :
return
else :
#  Variable which is used to store the result
first = -sys.maxsize
second = -sys.maxsize
i = 0
while (i < size) :
# compare the array element values
if (first < arr[i]) :
if (second < first) :
second = first

first = arr[i]

elif(second < arr[i]) :
second = arr[i]

i += 1

print("First Largest : ", first ,"\n", end = "")
print("Second Largest  : ", second ,"\n", end = "")

def main() :
obj = MyArray()
# Define collection of array elements
arr = [10, 3, 23, 86, 8, 9, 44, 5, 7]
# Get the size of array
size = len(arr)
obj.find_largest(arr, size)

if __name__ == "__main__": main()``````

#### Output

``````First Largest :  86
Second Largest  :  44``````
``````#   Ruby Program
#   Find the largest and second largest element in an array

class MyArray

# Find 2 largest element in array
def find_largest(arr, size)

if (size <= 1)

return
else

#  Variable which is used to store the result
first = -(2 ** (0. size * 8 - 2))
second = -(2 ** (0. size * 8 - 2))
i = 0
while (i < size)

# compare the array element values
if (first < arr[i])

if (second < first)

second = first
end
first = arr[i]
elsif(second < arr[i])

second = arr[i]
end
i += 1
end
# Display result
print("First Largest : ", first ,"\n")
print("Second Largest  : ", second ,"\n")
end
end
end
def main()

obj = MyArray.new()
# Define collection of array elements
arr = [10, 3, 23, 86, 8, 9, 44, 5, 7]
# Get the size of array
size = arr.length
obj.find_largest(arr, size)
end
main()``````

#### Output

``````First Largest : 86
Second Largest  : 44
``````
``````/*
Scala Program
Find the largest and second largest element in an array
*/
class MyArray
{
//Find 2 largest element in array
def find_largest(arr: Array[Int], size: Int): Unit = {
if (size <= 1)
{
return;
}
else
{
// Variable which is used to store the result
var first: Int = Int.MinValue;
var second: Int = Int.MinValue;
var i: Int = 0;
while (i < size)
{
//compare the array element values
if (first < arr(i))
{
if (second < first)
{
second = first;
}
first = arr(i);
}
else if (second < arr(i))
{
second = arr(i);
}
i += 1;
}
//Display result
print("First Largest : " + first + "\n");
print("Second Largest  : " + second + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyArray = new MyArray();
//Define collection of array elements
var arr: Array[Int] = Array(10, 3, 23, 86, 8, 9, 44, 5, 7);
//Get the size of array
var size: Int = arr.length;
obj.find_largest(arr, size);
}
}``````

#### Output

``````First Largest : 86
Second Largest  : 44``````
``````/*
Swift Program
Find the largest and second largest element in an array
*/
class MyArray
{
//Find 2 largest element in array
func find_largest(_ arr: [Int], _ size: Int)
{
if (size <= 1)
{
return;
}
else
{
// Variable which is used to store the result
var first: Int = Int.min;
var second: Int = Int.min;
var i: Int = 0;
while (i < size)
{
//compare the array element values
if (first < arr[i])
{
if (second < first)
{
second = first;
}
first = arr[i];
}
else if (second < arr[i])
{
second = arr[i];
}
i += 1;
}
print("First Largest : ", first ,"\n", terminator: "");
print("Second Largest  : ", second ,"\n", terminator: "");
}
}
}
func main()
{
let obj: MyArray = MyArray();
//Define collection of array elements
let arr: [Int] = [10, 3, 23, 86, 8, 9, 44, 5, 7];
//Get the size of array
let size: Int = arr.count;
obj.find_largest(arr, size);
}
main();``````

#### Output

``````First Largest :  86
Second Largest  :  44``````

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 