Created
July 19, 2017 20:11
-
-
Save abhilash0001/271506e32be281cea6e313197e251256 to your computer and use it in GitHub Desktop.
Move all zeros to end
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
// C# implementation with node traversing | |
public static int[] arr = {9, 1, 9, 0, 7, 0, 3, 5, 0}; | |
public int[] pushZerosToEnd(){ | |
var arrCount = arr.Count(); | |
var count = 0; | |
for (int i = 0; i < arrCount; i++){ | |
if (arr[i] != 0){ | |
arr[count++] = arr[i]; | |
} | |
} | |
while(count < arrCount) | |
arr[count++] = 0; | |
return arr; | |
} | |
var output = pushZerosToEnd(); | |
for (int x = 0; x < output.Count(); x++) | |
Console.WriteLine(output[x]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment