Jitendra Kumar
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.
By Jitendra Kumar in C# on Dec 11 2014
  • Manu Sree
    Dec, 2014 21

    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();}

    • 10
  • Harishsady D
    Dec, 2014 18

    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"-------------------------

    • 6
  • Abhijit  Kakade
    Dec, 2014 21

    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.

    • 5
  • Charmy George
    Jul, 2015 28

    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();}

    • 4
  • Upendra Pratap Shahi
    Jun, 2015 5

    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.

    • 2
  • Keith Fogle
    Dec, 2014 20

    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();}

    • 2
  • Dima D
    Jun, 2019 27

    static void Main(string[] args){int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };List largest = new List();int l1 = 0, l2 = 0, l3 = 0;for (int i = 0; i < a.Length; i++){if (a[i] > l1){l3 = l2;l2 = l1;l1 = a[i];}else if (a[i] > l2){l3 = l2;l2 = a[i];}else if (a[i] > l3){l3 = a[i];}}//displayArray.Sort(a);foreach (var item in a){Console.WriteLine(item);}Console.WriteLine("-----------");Console.WriteLine(l3);Console.Read();}

    • 1
  • Rahul Prajapat
    May, 2015 27

    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]);

    • 1
  • Vaibhav Singh
    Mar, 2015 11

    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.

    • 1
  • Kanniyappan Krish
    Jan, 2015 13

    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);

    • 1
  • kajal rane
    Apr, 2019 9

    public static void Main(string[] args){int[] a = { 12, 13, 24, 45, 65, 27, 37, 43, 41, 50, 75, 68, 98, 78 };Array.Reverse(a);Console.WriteLine("3rd largest no : " a[2]);}

    • 0
  • Keshav Chauhan
    Dec, 2016 30

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

    • 0
  • k vig
    Dec, 2016 13

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

    • 0
  • k vig
    Dec, 2016 13

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

    • 0
  • Awadhesh  Jha
    Dec, 2016 13

    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]);}

    • 0
  • Kanniyappan Krish
    Oct, 2016 26

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

    • 0
  • Joginder Banger
    Oct, 2016 3

    Thanks for sharing valuable question.

    • 0
  • Umesh  Kumar
    Aug, 2016 19

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

    • 0
  • Ashish Srivastava
    Jul, 2016 8

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

    • 0
  • Ashish Mishra
    Dec, 2015 24

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

    • 0
  • Chiranjeevi
    Dec, 2015 17

    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];

    • 0
  • Shivshanker Cheral
    Nov, 2015 24

    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);

    • 0
  • supraja kambam
    Nov, 2015 14

    C by l

    • 0
  • vipin kv
    Oct, 2015 6

    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));

    • 0
  • Salahuddin Farooqui
    Aug, 2015 13

    @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(); }

    • 0
  • Rohit Rana
    Jul, 2015 30

    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].

    • 0
  • Rohit Rana
    Jul, 2015 30

    /*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]);

    • 0
  • Charmy George
    Jul, 2015 28

    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();}

    • 0
  • Amrutanshu Panigrahi
    Jun, 2015 22

    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(); }

    • 0
  • Amrutanshu Panigrahi
    Jun, 2015 22

    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(); }

    • 0
  • Amrutanshu Panigrahi
    Jun, 2015 22

    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(); }

    • 0
  • Amrutanshu Panigrahi
    Jun, 2015 22

    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(); }

    • 0
  • Amrutanshu Panigrahi
    Jun, 2015 22

    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(); }

    • 0
  • Abhishek Ranjan
    Jun, 2015 14

    efe

    • 0
  • veera reddy
    Jun, 2015 6

    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]);}

    • 0
  • Safayat Zisan
    Apr, 2015 22

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

    • 0
  • Lalit Raghav
    Apr, 2015 9

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

    • 0
  • Pankaj  Kumar Choudhary
    Mar, 2015 23

    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]);

    • 0
  • Pankaj  Kumar Choudhary
    Mar, 2015 23

    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]);

    • 0
  • Ben Ben
    Mar, 2015 19

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

    • 0
  • Abrar Ahmad Ansari
    Mar, 2015 10

    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);}}}

    • 0
  • Deepti Abrol
    Feb, 2015 26

    ""

    • 0
  • Suresh Mogudala
    Jan, 2015 14

    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]);

    • 0
  • Venkatesh M
    Jan, 2015 12

    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(); }

    • 0
  • Nilesh Prajapati
    Jan, 2015 12

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

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS