Last active
September 21, 2021 15:21
-
-
Save rexcfnghk/c7a1c724146cb5393b19501ea24f25ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Math | |
{ | |
public static T SecondLargest<T>(IEnumerable<T> inputs) where T : IComparable<T> | |
{ | |
// Null checks skipped for brevity | |
var (largest, second) = (default(T), default(T)); | |
foreach (var input in inputs) | |
{ | |
if (input.CompareTo(largest) > 0) | |
{ | |
(largest, second) = (input, largest); | |
} | |
else if (input.CompareTo(second) > 0) | |
{ | |
second = input; | |
} | |
} | |
return second; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment