int[] array= new int[]{12,13,24,45,65,27,37,43,41,50,75,68};
var item = array.OrderByDescending(x=>x).Skip(2).Take(1).ToList();
foreach(int it in item)
Console.WriteLine(it);
Interviews12 answers
I have a array like {12,13,24,45,65,27,37,43,41,50,75,68}. Write a method or logic to get 3rd largest value in the array in only Single Loop is use without using sort.
12 Answers
sajid aliPosted Jul 29, 2019, 9:35 PM EDT
public int thirdlargestValue()
{
int first, second, third = 0;
int[] arr = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };
first = arr[0];
second = arr[1];
third = arr[2];
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] > first)
{
third = second;
second = first;
first = arr[i];
}
else if (arr[i] > second)
{
third = second;
second = arr[i];
}
else if (arr[i] > third)
{
third = arr[i];
}
}
return third;
}
Mahendra KumarPosted Apr 1, 2019, 10:56 PM EDT
int[] nums = new int[] {12,13,24,45,65,27,37,43,41,50,75,68}
var result = (from s in nums
select s).skip(2).first();
Bheema Shankar MaruvadaPosted Dec 24, 2018, 3:06 AM EST
var result = from n in number orderby n descending select n;
var value = result.ToList().ElementAt(2);
mukesh kumarPosted Oct 3, 2018, 5:01 AM EDT
class Test
{
public int Third_higest(int[] array)
{
// int[] array = new int[12] { 12, 13, 67, 45, 65, 27, 37, 43, 41, 50, 75, 68 };
int arrLen = array.Length - 1;
int indexofi = 0, temp = 0, count = 0, indexofj = 0, index = 0, breakpoint = 0;
while (true)
{
if (indexofj == arrLen)
{
if (indexofi != arrLen)
{
indexofi++;
}
if (count == 3)
{
break;
}
indexofj = 0;
}
if (array[indexofi] < array[indexofj + 1])
{
temp = array[indexofi];
array[indexofi] = array[indexofj + 1];
array[indexofj + 1] = temp;
temp = array[indexofi];
indexofj++;
}
else
{
indexofj++;
}
if (temp != 0 && indexofj == arrLen && indexofi == count)
{
array[indexofi] = 0;
count++;
}
// if (arrLen == index)
// {
// if (breakpoint == 0)
// {
// break;
// }
// index = 0;
// breakpoint = 0;
// }
// if (array[index] < array[index + 1])
// {
// int tmp = array[index];
// array[index] = array[index + 1];
// array[index + 1] = tmp;
// breakpoint = 1;
// }
// index++;
// temp++;
//}
}
return temp;
}
}
class Program
{
static void Main(string[] args)
{
Test test = new Test();
int[] array = new int[12] { 12,13,24,45,65,27,37,43,41,50,75,68};
int arrayOut = test.Third_higest(array);
Console.WriteLine(arrayOut);
Console.ReadLine();
}
}
Satyendra PatelPosted May 28, 2018, 7:46 AM EDT
for (var i = 1; i < array.Length;)
{
if (i != 0 && (array[i] < array[i - 1]))
{
var temp = array[i];
array[i] = array[i - 1];
array[i - 1] = temp;
i--;
}
else { i++; }
}
Surendra Pal SinghPosted Apr 27, 2018, 4:55 AM EDT
for (var i = 1; i < array.Length;)
{
if (i != 0 && (array[i] < array[i - 1]))
{
var temp = array[i];
array[i] = array[i - 1];
array[i - 1] = temp;
i--;
}
else { i++; }
}
Surendra Pal SinghPosted Apr 27, 2018, 4:54 AM EDT
for(int i=0;i<=array.Length-1;i )
{
if(array[i]
Naresh TottempudiPosted Feb 28, 2018, 9:03 PM EST
public void GetThirdHigestNumber()
{
int[] array = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };
var objResult = array.OrderByDescending(x => x).ToList().Take(3);
int thirdHighestNumber = 0;
foreach (int num in objResult)
{
thirdHighestNumber = num;
}
Console.WriteLine("Third Hieghest Number : {0}", thirdHighestNumber);
}
Sheel dUBEYPosted May 9, 2017, 9:01 PM EDT
public static int thirdLargestNumber(int[] numbers)
{
for(int i=0;i
Naveen KumarPosted Mar 19, 2017, 5:45 PM EDT
int iCount = 0;
int[] array = new int[12] { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };
for (int i = 0; i <= array.Length; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
if (array[i] > array[j])
{
iCount = array[i];
array[i] = array[j];
array[j] = iCount;
}
}
}
Console.WriteLine(array[2]);
Prasant MaharanaPosted Mar 1, 2017, 10:26 PM EST
int[] arr={12,13,24,45,65,27,37,43,41,50,75,68};
var q = arr.OrderByDescending(a => a).ToArray();
for (int i = 0; i < q.Count(); i++ )
{
if (i == 2)
{
Console.WriteLine(q[i].ToString());
break;
}
}
Rajiv LochanPosted Feb 24, 2017, 7:44 AM EST



