Remove leading zeros from an array
Given an array of N numbers, the task is to remove all leading zeros from the array.
Examples:
Input : arr[] = {0, 0, 0, 1, 2, 3}
Output : 1 2 3
Input : arr[] = {0, 0, 0, 1, 0, 2, 3}
Output : 1 0 2 3
Approach: Mark the first non-zero number’s index in the given array. Store the numbers from that index to the end in a different array. Print the array once all numbers have been stored in a different container.
Below is the implementation of the above approach:
C++
// C++ program to print the array // removing the leading zeros #include <bits/stdc++.h> using namespace std; // Function to print the array by // removing leading zeros void removeZeros(int a[], int n) { // index to store the first // non-zero number int ind = -1; // traverse in the array and find the first // non-zero number for (int i = 0; i < n; i++) { if (a[i] != 0) { ind = i; break; } } // if no non-zero number is there if (ind == -1) { cout << "Array has leading zeros only"; return; } // Create an array to store // numbers apart from leading zeros int b[n - ind]; // store the numbers removing leading zeros for (int i = 0; i < n - ind; i++) b[i] = a[ind + i]; // print the array for (int i = 0; i < n - ind; i++) cout << b[i] << " "; } // Driver Code int main() { int a[] = { 0, 0, 0, 1, 2, 0, 3 }; int n = sizeof(a) / sizeof(a[0]); removeZeros(a, n); return 0; } |
chevron_right
filter_none
Java
// Java program to print the array // removing the leading zeros import java.util.*; class solution { // Function to print the array by // removing leading zeros static void removeZeros(int[] a, int n) { // index to store the first // non-zero number int ind = -1; // traverse in the array and find the first // non-zero number for (int i = 0; i < n; i++) { if (a[i] != 0) { ind = i; break; } } // if no non-zero number is there if (ind == -1) { System.out.print("Array has leading zeros only"); return; } // Create an array to store // numbers apart from leading zeros int[] b = new int[n - ind]; // store the numbers removing leading zeros for (int i = 0; i < n - ind; i++) b[i] = a[ind + i]; // print the array for (int i = 0; i < n - ind; i++) System.out.print(b[i]+" "); } // Driver Code public static void main(String args[]) { int[] a = { 0, 0, 0, 1, 2, 0, 3 }; int n = a.length; removeZeros(a, n); } } |
chevron_right
filter_none
Python3
# Python3 program to print # the array removing # the leading zeros # Function to print the # array by removing # leading zeros def removeZeros(a, n): # index to store the # first non-zero number ind = -1; # traverse in the array # and find the first # non-zero number for i in range(n): if (a[i] != 0): ind = i; break; # if no non-zero # number is there if (ind == -1): print("Array has leading zeros only"); return; # Create an array to store # numbers apart from leading # zeros b[n - ind]; b=[0]*(n - ind); # store the numbers # removing leading zeros for i in range(n - ind): b[i] = a[ind + i]; # print the array for i in range(n - ind): print( b[i] , end=" "); # Driver Code a = [0, 0, 0, 1, 2, 0, 3]; n = len(a); removeZeros(a, n); # This code is contributed by mits |
chevron_right
filter_none
C#
// C# program to print the array // removing the leading zeros using System; class solution { // Function to print the array by // removing leading zeros static void removeZeros(int[] a, int n) { // index to store the first // non-zero number int ind = -1; // traverse in the array and find the first // non-zero number for (int i = 0; i < n; i++) { if (a[i] != 0) { ind = i; break; } } // if no non-zero number is there if (ind == -1) { Console.Write("Array has leading zeros only"); return; } // Create an array to store // numbers apart from leading zeros int[] b = new int[n - ind]; // store the numbers removing leading zeros for (int i = 0; i < n - ind; i++) b[i] = a[ind + i]; // print the array for (int i = 0; i < n - ind; i++) Console.Write(b[i]+" "); } // Driver Code public static void Main(String []args) { int[] a = { 0, 0, 0, 1, 2, 0, 3 }; int n = a.Length; removeZeros(a, n); } } // This code contributed by Rajput-Ji |
chevron_right
filter_none
PHP
<?php // PHP program to print // the array removing // the leading zeros // Function to print the // array by removing // leading zeros function removeZeros($a, $n) { // index to store the // first non-zero number $ind = -1; // traverse in the array // and find the first // non-zero number for ($i = 0; $i < $n; $i++) { if ($a[$i] != 0) { $ind = $i; break; } } // if no non-zero // number is there if ($ind == -1) { echo "Array has leading " . "zeros only"; return; } // Create an array to store // numbers apart from leading // zeros b[n - ind]; // store the numbers // removing leading zeros for ($i = 0; $i < $n - $ind; $i++) $b[$i] = $a[$ind + $i]; // print the array for ($i = 0; $i < $n - $ind; $i++) echo $b[$i] , " "; } // Driver Code $a = array(0, 0, 0, 1, 2, 0, 3); $n = sizeof($a); removeZeros($a, $n); // This code is contributed by ajit ?> |
chevron_right
filter_none
Output:
1 2 0 3
Recommended Posts:
- Maximum consecutive one’s (or zeros) in a binary array
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i]
- Maximum consecutive one’s (or zeros) in a binary circular array
- Move all zeros to start and ones to end in an Array of random integers
- Count number of trailing zeros in product of array
- Remove duplicate elements in an Array using STL in C++
- Remove minimum elements from the array such that 2*min becomes more than max
- Remove elements from the array which appear more than k times
- Remove an element to maximize the GCD of the given array
- Remove duplicates from sorted array
- Remove exactly one element from the array such that max - min is minimum
- Remove minimum elements from array so that max <= 2 * min
- Remove all occurrences of any element for maximum array sum
- Remove duplicates from an array of small primes
- Remove array end element to maximize the sum of product
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



