Giving an array of integers, such as [1, 0, 3, 4, 5, 0, ...],
provide a piece of code that returns the nonzero elements in the array, and reorder
the array with all these nonzero elements to the left, such as [1,3,4,5, X,X,X,X....]
the order of nonzero elements does not matter and the elements on the right of the array do not matter.
Reorder and count nonzero int in an array


this code will move all zero to the right,
public void MoveZeroes(int[] nums) { var stack = new Stack<int>(); int zeros = 0; for (var i = 0; i < nums.Length; i++) { if (nums[i] == 0) { zeros++; continue; } stack.Push(nums[i]); } for (var i = nums.Length  1; i > nums.Length  1  zeros; i) { nums[i] = 0; } for (var i = nums.Length 1  zeros; i >= 0; i) { nums[i] = stack.Pop(); } }

Actually, this is a duplicate of Move Zeros: https://leetcode.com/problems/movezeroes/.

this is similar to picking the pivot in quick sort, it's doable in one pass and O(1) space
int[] order(int[] a) { int i = 0; int j = a.length  1; while (i <= j) { while (j >= 0 && a[j] == 0) j; while (i < a.length && a[i] != 0) i++; if(i <= j) { a[i] = a[i] ^ a[j]; a[j] = a[i] ^ a[j]; a[i] = a[i] ^ a[j]; } } return Arrays.copyOfRange(a, 0, i); }