-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindThesecondLargest.cs
44 lines (38 loc) · 1.12 KB
/
FindThesecondLargest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
namespace ConsoleApplication23
{
internal static class Program
{
public static void Main(string[] args)
{
int[] arr = {12, 25, 32, 14, 25, 56, 54, 23, 12};
var length = arr.Length;
var b= FindSecondLargestNumber(arr, length);
Console.WriteLine("The 2nd Largest No. is " + b );
}
private static int FindSecondLargestNumber(IReadOnlyList<int> arr, int length)
{
int i, second;
if (length < 2)
{
Console.WriteLine("Invalid Input");
return -1;
}
var first = second =0;
for ( i = 0; i < length; i++)
{
if (arr[i] > first)
{
second = first;
first = arr[i];
}
else if (arr[i] > second && arr[i] != first)
{
second = arr[i];
}
}
return second;
}
}
}