Created
February 20, 2019 00:03
-
-
Save WennderSantos/ac6f2035e71a63a88a974eb626e502e0 to your computer and use it in GitHub Desktop.
Find smallest and second smallest elements in an array
This file contains hidden or 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
| 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