Javatpoint Logo
Javatpoint Logo

Move all Zeroes to End of Array

Arrays are utilized to store various qualities in a single variable, rather than proclaiming separate variables for each value.

We can perform numerous operations on the given Array. However, presently we will take care of on the issue with which we need to move all the zeroes in an array to its end.

Problem Statement:

In given array our task is to push all the zeroes of a given array to its end of the array.


We have many approaches to perform this task. Some of the approaches are:


Using For Loop:

Move all Zeroes to End of Array


  1. The code imports key libraries, including ArrayList and Groups from the java.util pack. A class named MoveZero is portrayed.
  2. With the information in the Arrays.asList, an array ArrayList is created and installed. On this array, the moveZeroes method is called.
  3. The altered array has been published.
  4. The moveZeroes procedure is depicted to move all zeroes to the end farthest reaches of the rundown.
  5. It underlines through the rundown from the finish to the start.
  6. remove(i) is utilized to eliminate the ongoing component from its unique situation if it is zero.
  7. add(0) is utilized to add a zero to the furthest limit of the list.
  8. The changed list is engraved in the principal technique.

Time Complexity: O(n)

Space Complexity: O(1)


Using While Circle:

Move all Zeroes to End of Array


  1. Initialize Pointers: Two pointers left (l) and right (r) are initialized at the beginning and end of the array, as follows.
  2. Loop Condition: The while loop is running until the index l is less than the index r.
  3. Check and Swap: The loop has a built-in condition check that verifies if arr[l] is the element from the left index and arr[r] id the element from the right index. Otherwise, it renews its components.
  4. Move Pointers: At the end of the swap or when the requirements are not fulfilled it adjusts them. The l is incremented if the element of the left index is not zero. If the element located at the right index is zero, r is decremented.
  5. Loop Continues: The procedures stop when the left pointer goes past the right pointer.
  6. Display Result: In the last step, the original array is altered and the new array is displayed.

Time Complexity: O(n)

Space Complexity: O(1)


Using an ArrayList to dynamically manage the array elements.


  1. The code starts by getting central libraries to play out a couple of fundamental undertakings in Java.
  2. It specifies a class called MoveZero.
  3. In the head or fundamental technique, an occasion of using the class is shown.
  4. An ArrayList named arrays is made and presented with values gave in the Arrays.asList.
  5. The moveZero technique is then called this array.
  6. The changed array is printed.
  7. The moveZeroToEnd technique is portrayed to revise the parts in the ArrayList by moving all zeroes beyond what many would consider possible (finish of the list).
  8. It sorts the components in the input list into two distinct lists: zeros and nonzeroes.
  9. The final result is gotten by connecting the nonZeros and zeros lists.
  10. The changed list is then gotten back from the method.

Time Complexity: O(n)

Space Complexity: O(n)


Move all Zeroes to End of Array


In general, each solution offers a different approach to addressing the problem, with varying complexities in terms of time and space. The choice of plan depends upon explicit necessities and restrictions in a given circumstance.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA