Advertisement
haikelfazzani

MOVE ALL ZEROES TO END OF ARRAY

Aug 26th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. // Move Zero To The End Of Array : Input => Array [0, 1, 0, 3, 12] Output => Array [1, 3, 12, 0, 0].
  2.  
  3. public class App {
  4.    
  5.     public static void moveZero(int[] myArray) {
  6.        
  7.         int index = 0;
  8.        
  9.         for(int i=0; i < myArray.length;i++) {
  10.            
  11.             if(myArray[i] != 0) {
  12.                
  13.                 myArray[index++] = myArray[i]; // 1 3 12
  14.                
  15.             }
  16.            
  17.         }
  18.        
  19.         while(index < myArray.length) {
  20.            
  21.             myArray[index++] = 0; // 1 3 12 0 0
  22.            
  23.         }
  24.        
  25.         for(int i =0 ; i < myArray.length;i++) {
  26.             System.out.print(myArray[i] + " , ");
  27.         }
  28.     }
  29.  
  30.     public static void main(String[] args) {
  31.        
  32.         int[] myArray = {0, 1, 0, 3, 12};
  33.        
  34.         moveZero(myArray);
  35.        
  36.        
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement