Skip to content

Instantly share code, notes, and snippets.

@rexcfnghk
Last active September 21, 2021 15:21
Show Gist options
  • Save rexcfnghk/c7a1c724146cb5393b19501ea24f25ea to your computer and use it in GitHub Desktop.
Save rexcfnghk/c7a1c724146cb5393b19501ea24f25ea to your computer and use it in GitHub Desktop.
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