43
Reply

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.

Jitendra Kumar

Jitendra Kumar

9y
10.7k
1
Reply

    static void Main(string[] args){int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };Array.Sort(a);Console.WriteLine(a[2]);Console.Read();}

    static void Main(string[] args) { int[] a = {12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68,98,78 }; int a1 = 0, a2 = 0, a3 = 0,a4=0; foreach (int item in a) { if (item > a1) { a3 = a2; a2 = a1; a1 = item; } if (item < a1 && item > a2) { a3 = a2; a2 = item; } if (item < a1 && item < a2 && item > a3) { a3 = item; } } Console.WriteLine(a1 "---------" a2 "--------" a3 "--------" a4); Console.ReadLine(); } ------------------ you will get your answer in "a3"-------------------------

    Hi Jitendra, Don't need loop here.. if its array follow below steps. 1) Array.Sort(Arrayname); 2) Print -- ArrayName[2] check this and let me know.

    static void Main(string[] args){int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 682, 982, 782 };Array.Sort(a);Array.Reverse(a);Console.WriteLine(a[2]);Console.Read();}

    static void Main(string[] args) { int[] arrayToSort = { 12,13,24,45,65,27,37,43,41,50,75,68 }; Array.Sort(arrayToSort ); Console.WriteLine(arrayToSort [2]); Console.Read(); }because array start from 0 index and you want 3rd largest.

    static void Main(string[] args){int[] a = {12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };List a_list = new List { };for(int i = 0; i < a.Length; i++){a_list.Add(a[i]);}// find and remove the first two largest numbers in arrayint greatest_int = a_list.Max();a_list.Remove(greatest_int);greatest_int = a_list.Max();a_list.Remove(greatest_int);//print the resultConsole.WriteLine("Third largest number in array: " + a_list.Max());Console.ReadLine();}

    int[] Ar = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 }; Array.Sort(Ar); Console.WriteLine(Ar[Ar.Length-3]);

    Note that almost all answers below (except Harish Sady's) use the built in C# .net functions to first 'sort' the array. Sorting the array will take O(n log n) time. Instead, simply use 'three' passes of Bubble Sort to get the 3rd largest number in O(n) time.

    int[] items = { 12,13,24,45,65,27,37,43,41,50,75,68}; int result= items.OrderByDescending(x => x).Skip(2).First(); Console.WriteLine("Third largest value is: {0} ", result);

    Array.Sort(arr);Array.Reverse(arr); Console.WriteLine(arr[2]);

    write code for bubble sort to get highest array value then write arr[2];

    write code for bubble sort to get highest array value then write arr[2];

    public void getarray(){int[] a = { 12,13,24,45,65,27,37,43,41,50,75,68 };Array.Sort(a);Array.Reverse(a);Response.Write(a[2]);}

    int[] Numbers = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 }; int Result = Numbers.OrderByDescending(x => x).Skip(2).First();

    Thanks for sharing valuable question.

    You can also achieve using Linq query int a=arry.OrderBy(x => x).Skip(2).Take(1).SingleOrDefault();

    Array.Soft(a); Response.Write(a[2]);

    Dear sir manu shree for second largest printConsole.WriteLine(a[a.Length-2]); Console.Read();

    int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };Array.Sort(a);int maxIndex = a[a.Length-3];

    List numbers = new List() { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };var highest = (from n in numbersorderby nselect n).Skip(4).Take(1);foreach (var n in highest)Console.WriteLine("Value: " + n);

    C by l

    int first = 0; int second = 0; int third = 0; for (int i = 0; i < arr.Length; i++) {if (arr[i] > first){third = second;second = first;first = arr[i];}if (arr[i] < first && arr[i] > second){third = second;second = arr[i];}if (arr[i] < first && arr[i] < second && arr[i] > third){third = arr[i];}} Console.WriteLine(string.Format("Largest:{0} , SecondLargest:{1} , ThirdLargest:{2}", first, second, third));

    @Swetha's Answer will not work here.we need to add Array.Reverse() as follows. static void Main(string[] args) { int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 }; Array.Sort(a); Array.Reverse(a); Console.WriteLine(a[2]); Console.Read(); }

    Swetha Gandla and Abhijit Kakade if you aill execute your code the output will 3rd smallest value first you have to reverse the array or print Array_Name[N-2].

    /*You can find value without using any loop Suppose the size of your loop is N. call*/ Array.Sort(Array_Name); Console.WriteLine(Array_Name[N-2]);

    static void Main(string[] args){int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 682, 982, 782 };Array.Sort(a);Array.Reverse(a);Console.WriteLine(a[2]);Console.Read();}

    static void Main(string[]args) { int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 }; Array.Sort(a); Console.WriteLine(a[2]); Console.Read(); }

    static void Main(string[] args) { int[] arrayToSort = { 12,13,24,45,65,27,37,43,41,50,75,68 }; Array.Sort(arrayToSort ); Console.WriteLine(arrayToSort [2]); Console.Read(); }

    static void Main(string[] args) { int[] arrayToSort = { 12,13,24,45,65,27,37,43,41,50,75,68 }; Array.Sort(arrayToSort ); Console.WriteLine(arrayToSort [2]); Console.Read(); }

    static void Main(string[] args) { int[] arrayToSort = { 12,13,24,45,65,27,37,43,41,50,75,68 }; Array.Sort(arrayToSort ); Console.WriteLine(arrayToSort [2]); Console.Read(); }

    static void Main(string[] args) { int[] arrayToSort = { 12,13,24,45,65,27,37,43,41,50,75,68 }; Array.Sort(arrayToSort ); Console.WriteLine(arrayToSort [2]); Console.Read(); }

    efe

    static void Main(){Console.WriteLine("Array initialization");int[] arr = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };Console.WriteLine("array reversing");Array.Reverse(arr);foreach (int i in arr)Console.WriteLine(i);Console.WriteLine("Third Largest Value is :" + arr[2]);}

    List aList = new List; aList.add(array); aList.sort(); foreach(int a in aList) {int i = a[array.length-3]; } console.writeline(i);

    int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 }; Array.Sort(a); Array.Reverse(a) a[2]

    int[] Ar = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };Array.Sort(Ar);Console.WriteLine(Ar[Ar.Length-3]);

    int[] Ar = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };Array.Sort(Ar);Console.WriteLine(Ar[Ar.Length-3]);

    int[] arr = {12,20,31,4,50,63,43,22,18 }; Array.Reverse(arr); int thirdhighestvalue = arr[2];

    class Program{static void Main(string[] args){int []array = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };var maxthreevalue= array.AsEnumerable().OrderByDescending(o=>o).Take(3).ToArray();foreach (var display in maxthreevalue){Console.WriteLine(display);}}}

    ""

    List list =new List() { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 }; list.Sort(); Console.WriteLine(" Get 3rd Larget value is "+ list[list.Count-3]);

    static void Main(string[] args){int[] arr = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 };var thirdHighest = (from first in ((from third in arr orderby third descending select third).Take(3)) orderby first ascending select first).Take(1);Console.WriteLine("3rd largest value:{0}",thirdHighest.ToArray()[0]);Console.ReadKey(); }

    int[] values = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68 }; Array.Sort(values); print(values[values.Count()-3].ToString());