Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now

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 »

Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now