Skip to content

Instantly share code, notes, and snippets.

@abhilash0001
Created July 19, 2017 20:11
Show Gist options
  • Save abhilash0001/271506e32be281cea6e313197e251256 to your computer and use it in GitHub Desktop.
Save abhilash0001/271506e32be281cea6e313197e251256 to your computer and use it in GitHub Desktop.
Move all zeros to end
// 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