Skip to content

Instantly share code, notes, and snippets.

@WennderSantos
Created February 20, 2019 00:03
Show Gist options
  • Select an option

  • Save WennderSantos/ac6f2035e71a63a88a974eb626e502e0 to your computer and use it in GitHub Desktop.

Select an option

Save WennderSantos/ac6f2035e71a63a88a974eb626e502e0 to your computer and use it in GitHub Desktop.
Find smallest and second smallest elements in an array
static void Main(string[] args)
{
var input = new int[] { 1, 4, 3, 5, 6, 7, 8, 1 };
var smallest = input[0];
var secondSmallest = input[1];
if(smallest > secondSmallest)
{
var aux = smallest;
smallest = secondSmallest;
secondSmallest = aux;
}
for (var i = 2; i < input.Length; i++)
{
if (smallest > input[i])
{
secondSmallest = smallest;
smallest = input[i];
}
else if (secondSmallest > input[i] && input[i] != smallest)
{
secondSmallest = input[i];
}
}
Console.WriteLine(smallest);
Console.WriteLine(secondSmallest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment