Remove repeated digits in a given number Last Updated : 27 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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++ // 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 program int main() { long int n = 12224; cout << removeRecur(n); return 0; } Java // Java program to remove repeated digits import 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. Python3 # 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 Code if __name__ == '__main__': n = 12224 print(removeRecur(n)) # This code is contributed by # Surendra_Gangwar C# // C# program to remove repeated digits using 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 // 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. ?> JavaScript <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> Output124 Time Complexity: O(log10n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Remove repeated digits in a given number K kartik Follow Improve Article Tags : Strings Stack Mathematical DSA number-digits +1 More Practice Tags : MathematicalStackStrings Similar Reads Remove recurring digits in a given number Given a number as string, remove recurring digits from the given string. The changes must be made in-place. Expected time complexity O(n) and auxiliary space O(1).Examples: Input: num[] = "1299888833" Output: num[] = "12983" Input: num[] = "1299888833222" Output: num[] = "129832" We strongly recomme 7 min read Minimum digits to remove to make a number Perfect Square Given an integer n, we need to find how many digits remove from the number to make it a perfect square. Examples : Input : 8314 Output: 81 2 Explanation: If we remove 3 and 4 number becomes 81 which is a perfect square. Input : 57 Output : -1 The idea is to generate all possible subsequences and ret 9 min read Find the frequency of a digit in a number Given a number N and a digit D. Write a program to find how many times the digit D appears in the number N. Examples : Input: N = 1122322 , D = 2 Output: 4 Input: N = 346488 , D = 9 Output: 0 The idea to solve this problem is to keep extracting digits from the number N and check the extracted digits 4 min read Smallest number by rearranging digits of a given number Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of a given number. Examples: Input: n = 846903Output: 304689Input: n = 55010Output: 10055Input: n = -40505Output: -55400Steps to find the smallest number. Count the frequency of each digit in the number.If i 7 min read Remove leading zeros from a Number given as a string Given numeric string str, the task is to remove all the leading zeros from a given string. If the string contains only zeros, then print a single "0". Examples: Input: str = "0001234" Output: 1234 Explanation: Removal of leading substring "000" modifies the string to "1234". Hence, the final answer 7 min read Like