The user will input an unsorted integer array and the method should find the third largest integer in the array.
- input: 3 ,6, 9, 8, 0
The user will input an unsorted integer array and the method should find the third largest integer in the array.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Uday DodiyaRank #239 · 10.3K reputationPosted Nov 16, 2022, 1:17 AM EST
public class ThirdLargestInArrayExample{ public static int getThirdLargest(int[] a, int total){ int temp; for (int i = 0; i a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } return a[total-3]; } public static void main(String args[]){ int c[]={3,6,9,8,0}; int a[]={1,2,5,6,3,2}; int b[]={44,66,99,77,33,22,55}; System.out.println("Third Largest: "+getThirdLargest(c,5)); System.out.println("Third Largest: "+getThirdLargest(a,6)); System.out.println("Third Largest: "+getThirdLargest(b,7)); }}Output :
Amit MohantyRank #112 · 20K reputationPosted Nov 16, 2022, 1:21 AM EST
Try this
int[] array = { 3, 6, 9, 8, 0 }; Array.Sort(array); int elementCount = 0; int? thirdLargestNumber = null; foreach (var element in array) { var temp = array.Skip(elementCount).ToArray(); if (temp.Length == 3) { thirdLargestNumber = temp[0]; break; } elementCount++; } Console.WriteLine($"Third largest number = {thirdLargestNumber}");Jignesh KumarRank #91 · 22.9K reputationPosted Nov 16, 2022, 3:39 AM EST
Please check below one,
int largestNumberAtPosion = 3; int[] intArray = { 3, 6, 9, 8, 0 }; var sortedArray = intArray.OrderByDescending(x => x); var topNumber = sortedArray.Skip(largestNumberAtPosion - 1).Take(1).FirstOrDefault();Aravind GovindarajRank #430 · 5.8K reputationPosted Nov 16, 2022, 5:57 PM EST
Pls follow the steps
1.get it as list
2. Sort it as descending using lambda
3. Then apply Skip(2).Take(1) => It will skip 0,1 index and take 2 index value.
4. Another option after skip apply firstordefaultvaule.
Both yield the same result
Vishal YelveRank #178 · 13.6K reputationPosted Nov 16, 2022, 2:27 AM EST
Try this
public class Program { public static void Main() { int[] unsortedArray = new int[5] {3 ,6, 9, 8, 0}; Console.WriteLine("Input UnSorted Array: "); Console.WriteLine("------------------------------------------------ "); for( int i = 0; i < unsortedArray.Length; i++) { Console.WriteLine(unsortedArray[i]); } Console.WriteLine("\n\nThird Largest Element : " + ThirdLargetElement(unsortedArray)); } static int ThirdLargetElement(int[] array) { int large = 0, small = 0, mid = 0; for(int i = 0; i < array.Length; i++) { if (large < array[i] && mid <= large) { small = mid; mid = large; large = array[i]; } else if (mid < array[i] && small <= mid) { small = mid; mid = array[i]; } else if (small <= array[i]) { small = array[i]; } } return small; } } OutPut Input UnSorted Array: ------------------------------------------------ 3 6 9 8 0 Third Largest Element : 6