Remove leading zeros from an array

01 Nov, 2021
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++
Java
Python3
C#
PHP
Javascript
// 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 zerosvoid 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 Codeint main(){    int a[] = { 0, 0, 0, 1, 2, 0, 3 };    int n = sizeof(a) / sizeof(a[0]);    removeZeros(a, n);    return 0;}
// Java program to print the array// removing the leading zerosimport java.util.*; class solution{ // Function to print the array by// removing leading zerosstatic 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 Codepublic static void main(String args[]){    int[] a = { 0, 0, 0, 1, 2, 0, 3 };    int n = a.length;    removeZeros(a, n);     }}
# Python3 program to print# the array removing# the leading zeros # Function to print the# array by removing# leading zerosdef 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 Codea = [0, 0, 0, 1, 2, 0, 3];n = len(a);removeZeros(a, n); # This code is contributed by mits
// C# program to print the array// removing the leading zerosusing System; class solution{ // Function to print the array by// removing leading zerosstatic 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 Codepublic 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
<?php// PHP program to print// the array removing// the leading zeros // Function to print the// array by removing// leading zerosfunction 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?>
<script> // Javascript 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        var 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) {            document.write("Array has leading zeros only");            return;        }         // Create an array to store        // numbers apart from leading zeros        var b = Array(n - ind).fill(0);         // 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++)            document.write(b[i] + " ");    }     // Driver Code             var a = [ 0, 0, 0, 1, 2, 0, 3 ];        var n = a.length;        removeZeros(a, n);  // This code is contributed by todaysgaurav </script>
1 2 0 3
 
jit_t
sahilshelangia
Rajput-Ji
Mithun Kumar
todaysgaurav
Kirti_Mangal
school-programming
Arrays
School Programming
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
What is Data Structure: Types, Classifications and Applications
Chocolate Distribution Problem
Python Dictionary
Reverse a string in Java
Introduction To PYTHON
Interfaces in Java
Inheritance in C++