Advertisement
klippa

Second Largest

Aug 19th, 2022
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.28 KB | None | 0 0
  1. int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };
  2. int largest = int.MinValue;
  3. int second = int.MinValue;
  4. foreach (int i in myArray)
  5. {
  6.  if (i > largest)
  7.  {
  8.   second = largest;
  9.   largest = i;
  10.  }
  11. else if (i > second)
  12.     second = i;
  13. }
  14.  
  15. System.Console.WriteLine(second);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement