Move zeros

using System; using System.Linq; public class Test { public static void Main() { int[] nums = new int[] { 0, 3, 12, 0, 0, 0, 4, 5}; int j = 0; for(int i = 0; i < nums.Length; i++) { if(nums[i] != 0) { nums[j++] = nums[i]; } Print(nums); } for(int i = j; i < nums.Length; i++) { nums[i] = 0; } Print(nums); } public static void Print(int[] nums) { Console.WriteLine(string.Join(",", nums.Select(x=>x.ToString()).ToArray())); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.