
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you separate zeros from non-zeros in an integer array using Java?
To separate zeros from non-zeros in an integer array, and push them to the end, you need to rearrange it array by assigning all the nonzero elements to its positions, sequentially, starting from zero. Then, from last position of the array to its end populate it with zeros.
Example
Following Java program pushes all the zeros in an array to its end.
import java.util.Arrays; import java.util.Scanner; public class ZerosFromNonZeros { public static void main(String args[]){ //Reading the array from the user Scanner sc = new Scanner(System.in); System.out.println("Enter the size of the array that is to be created: "); int size = sc.nextInt(); int[] myArray = new int[size]; System.out.println("Enter the elements of the array: "); for(int i=0; i<size; i++){ myArray[i] = sc.nextInt(); } System.out.println("The array created is: "+Arrays.toString(myArray)); System.out.println("Resultant array: "); int pos = 0; for(int i=0; i<myArray.length; i++){ if(myArray[i]!=0){ myArray[pos]=myArray[i]; pos++; } } while(pos<myArray.length) { myArray[pos] = 0; pos++; } System.out.println("The array created is: "+Arrays.toString(myArray)); } }
Output
Enter the size of the array that is to be created: 8 Enter the elements of the array: 14 0 56 0 12 47 0 0 The array created is: [14, 0, 56, 0, 12, 47, 0, 0] Resultant array: The array created is: [14, 56, 12, 47, 0, 0, 0, 0]
In the same way to place the zeros at the starting of the array, iterate the elements of the array backwards, arrange each non-zero element in the array sequentially starting from the last position. Finally, fill the remaining positions with zeros.
rearrange it array by assigning all the nonzero elements to its positions, sequentially, starting from zero. Then, from last position of the array to its end populate it with zeros.
Example
Following Java program pushes all the zeros in an array to the start.
import java.util.Arrays; import java.util.Scanner; public class ZerosFromNonZeros { public static void main(String args[]){ //Reading the array from the user Scanner sc = new Scanner(System.in); System.out.println("Enter the size of the array that is to be created: "); int size = sc.nextInt(); int[] myArray = new int[size]; System.out.println("Enter the elements of the array: "); for(int i=0; i<size; i++){ myArray[i] = sc.nextInt(); } System.out.println("The array created is: "+Arrays.toString(myArray)); System.out.println("Resultant array: "); int pos = myArray.length-1; for(int i = myArray.length-1; i>=0; i--){ if(myArray[i]!=0){ myArray[pos]=myArray[i]; pos--; } } while(pos>=0) { myArray[pos] = 0; pos--; } System.out.println("The array created is: "+Arrays.toString(myArray)); } }
Output
Enter the size of the array that is to be created: 8 Enter the elements of the array: 14 0 56 0 12 47 0 0 The array created is: [14, 0, 56, 0, 12, 47, 0, 0] Resultant array: The array created is: [0, 0, 0, 0, 14, 56, 12, 47]
- Related Questions & Answers
- Remove Leading Zeros from an Array using C++
- Left pad an integer in Java with zeros
- Remove Leading Zeros From String in Java
- How do you fill in or pad a column with zeros using a MySQL query?
- Create an integer column in an R data frame with leading zeros
- Remove leading zeros in array - JavaScript
- How to move all the zeros to the end of the array from the given array of integer numbers using C#?
- Removing leading zeros from a String using apache commons library in Java
- Remove leading zeros in array in JavaScript
- Remove leading zeros in a JavaScript array?
- Remove Trailing Zeros from string in C++
- Python program to remove leading zeros from an IP address
- Java Program to Remove leading zeros
- How do you convert an ArrayList to an array in Java?
- Duplicate Zeros in Python