Move zeros to end of array in C#

 

Move zeros to end of array in C#

C# Code

        private static void MoveZeroToEnd()

        {

            int[] input = { 1, 2, 0, 0, 6, 10, 9 };

            int index = 0;

            for (int i = 0; i < input.Length; i++)

            {

                if (input[i] != 0)

                {

                    input[index] = input[i];

                    index++;

                }

            }

            for (int i = index; i < input.Length; i++)

            {

                input[i] = 0;

            }

 

            foreach (var item in input)

            {

                Console.WriteLine(item);

            }

        }


Output



Share this

Related Posts

Previous
Next Post »