In Previous Post Write a program to move all negative elements to end in an int array ? we learned how to move all negative elements to end in an Array.
In this post, we will learn to write a program to move all zero at the end of the array with/without using existing data structures?
Approach 1: Write a program to move all zero at the end of the array without using existing data structures
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.kkjavatutorials.client; /** * @author KK JavaTutorials *Write a program to move all zero at the end of the array without using existing data structures */ public class MoveZerosInLast_1 { public static void main(String[] args) { //Input Array int inputArr[] = {10, 2, 78, 0, 0, 30, 19, 0, 16, 17, 80, 13, 20, 0, 6}; int inputArrLength = inputArr.length; int indexCount = 0; for (int i = 0; i < inputArr.length; i++) { //First of all storing all non negative elements in same input array if(inputArr[i] !=0) { inputArr[indexCount++] = inputArr[i]; } } //Now storing all elements with zero value while(indexCount < inputArrLength) { inputArr[indexCount++] = 0; } //Final output array for (int data : inputArr) { System.out.print(data+" "); } } } |
Output of this Program:
10 2 78 30 19 16 17 80 13 20 6 0 0 0 0
Approach 2: Write a program to move all zero at the end of the array with using existing data structures
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.kkjavatutorials.client; import java.util.ArrayList; import java.util.List; /** * @author KK JavaTutorials *Write a program to move all zero at the end of the array with using existing data structures */ public class MoveZerosInLast_2 { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); int inputArr[] = {10, 2, 78, 0, 0, 30, 19, 0, 16, 17, 80, 13, 20, 0, 6}; int count = 0; for (int data : inputArr) { //First of all storing all non negative elements in list if(data !=0) { list.add(data); }else { //Increment count for every element with zero value count++; } } //Finally store zero in the list for (int i = 1; i <=count ; i++) { list.add(0); } //Printing final output Integer[] outputArray = list.toArray(new Integer[list.size()]); for (Integer data : outputArray) { System.out.print(data+" "); } } } |
Output of this Program:
10 2 78 30 19 16 17 80 13 20 6 0 0 0
You May Also Like:
How to convert number to words in java
How to swap two numbers with or without temporary variable in java
Java Program to Swap two numbers using Bitwise XOR Operator?
How to reverse a number in Java
How to check Armstrong number java program
Java program to find factorial of a number
Java Program to Calculate the Power of a Number
Check whether a number is prime or not
Java Program to displaying prime numbers
Fibonacci series using iterative and recursive approach java program
Find largest and second largest number in the given Array
Java program to Remove Duplicate Elements From an Array
Find common elements between two Arrays
Find largest and smallest number in the given Array
That’s all about Write a program to move all zero at the end of the array with/without using existing data structures ?
If you have any feedback or suggestion please feel free to drop in blow comment box.