Remove repeated digits in a given number - GeeksforGeeks

20 Jan, 2022
Given an integer, remove consecutive repeated digits from it.Examples:  
Input: x = 12224
Output: 124

Input: x = 124422
Output: 1242

Input: x = 11332
Output: 132
We need to process all digits of n and remove consecutive representations. We can go through all digits by repeatedly dividing n with 10 and taking n%10.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to remove repeated digits#include <iostream>using namespace std; long int removeRecur(long int n){    // Store first digits as previous digit    int prev_digit = n % 10;     // Initialize power    long int pow = 10;    long int res = prev_digit;     // Iterate through all digits of n, note that    // the digits are processed from least significant    // digit to most significant digit.    while (n) {        // Store current digit        int curr_digit = n % 10;         if (curr_digit != prev_digit) {            // Add the current digit to the beginning            // of result            res += curr_digit * pow;             // Update previous result and power            prev_digit = curr_digit;            pow *= 10;        }         // Remove last digit from n        n = n / 10;    }     return res;} // Driver programint main(){    long int n = 12224;    cout << removeRecur(n);    return 0;}
// Java program to remove repeated digitsimport java.io.*; class GFG {     static long removeRecur(long n)    {                 // Store first digits as previous        // digit        long prev_digit = n % 10;             // Initialize power        long pow = 10;        long res = prev_digit;             // Iterate through all digits of n,        // note that the digits are        // processed from least significant        // digit to most significant digit.        while (n>0) {                         // Store current digit            long curr_digit = n % 10;                 if (curr_digit != prev_digit)            {                // Add the current digit to                // the beginning of result                res += curr_digit * pow;                     // Update previous result                // and power                prev_digit = curr_digit;                pow *= 10;            }                 // Remove last digit from n            n = n / 10;        }             return res;    }         // Driver program    public static void main (String[] args)    {        long n = 12224;                 System.out.println(removeRecur(n));    }} // This code is contributed by anuj_67.
# Python 3 program to remove repeated digits def removeRecur(n):         # Store first digits as previous digit    prev_digit = n % 10     # Initialize power    pow = 10    res = prev_digit     # Iterate through all digits of n, note    # that the digits are processed from    # least significant digit to most    # significant digit.    while (n):                 # Store current digit        curr_digit = n % 10         if (curr_digit != prev_digit):                         # Add the current digit to the            # beginning of result            res += curr_digit * pow             # Update previous result and power            prev_digit = curr_digit            pow *= 10         # Remove last digit from n        n = int(n / 10)         return res # Driver Codeif __name__ == '__main__':    n = 12224    print(removeRecur(n)) # This code is contributed by# Surendra_Gangwar
// C# program to remove repeated digitsusing System; class GFG {     static long removeRecur(long n)    {                 // Store first digits as previous        // digit        long prev_digit = n % 10;             // Initialize power        long pow = 10;        long res = prev_digit;             // Iterate through all digits of n,        // note that the digits are        // processed from least significant        // digit to most significant digit.        while (n > 0) {                         // Store current digit            long curr_digit = n % 10;                 if (curr_digit != prev_digit)            {                // Add the current digit to                // the beginning of result                res += curr_digit * pow;                     // Update previous result                // and power                prev_digit = curr_digit;                pow *= 10;            }                 // Remove last digit from n            n = n / 10;        }             return res;    }         // Driver program    public static void Main ()    {        long n = 12224;                 Console.WriteLine(removeRecur(n));    }} // This code is contributed by anuj_67.
<?php// PHP program to remove// repeated digits function removeRecur($n){         // Store first digits    // as previous digit    $prev_digit = $n % 10;     // Initialize power    $pow = 10;    $res = $prev_digit;     // Iterate through all digits    // of n, note that the digits    // are processed from least    // significant digit to most    // significant digit.    while ($n)    {                 // Store current digit        $curr_digit = $n%10;         if ($curr_digit != $prev_digit)        {            // Add the current digit            // to the beginning of            // result            $res += $curr_digit * $pow;                 // Update previous result            // and power            $prev_digit = $curr_digit;            $pow *= 10;        }         // Remove last digit        // from n        $n = $n / 10;    }     return $res;}     // Driver Code    $n = 12224;    echo removeRecur($n);     // This code is contributed by ajit.?>
<script>     // Javascript program to    // remove repeated digits         function removeRecur(n)    {                   // Store first digits as previous        // digit        let prev_digit = n % 10;               // Initialize power        let pow = 10;        let res = prev_digit;               // Iterate through all digits of n,        // note that the digits are        // processed from least significant        // digit to most significant digit.        while (n > 0) {                           // Store current digit            let curr_digit = n % 10;                   if (curr_digit != prev_digit)            {                // Add the current digit to                // the beginning of result                res += curr_digit * pow;                       // Update previous result                // and power                prev_digit = curr_digit;                pow *= 10;            }                   // Remove last digit from n            n = parseInt(n / 10, 10);        }               return res;    }         let n = 12224;                 document.write(removeRecur(n));     </script>
Output: 
124
Time Complexity: O(log10n)
Auxiliary Space: O(1)
This article is contributed by Kartik. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
jit_t
vt_m
SURENDRA_GANGWAR
Akanksha_Rai
divyeshrabadiya07
subham348
surinderdawra388
number-digits
Mathematical
Stack
Strings
Strings
Mathematical
Stack
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
Program for Decimal to Binary Conversion
Stack Data Structure (Introduction and Program)
Stack Class in Java
Stack in Python
Check for Balanced Brackets in an expression (well-formedness) using Stack
Program for Tower of Hanoi