In this article, we are given a number n and we need to remove duplicate numbers in the given number.
Input: x = 12224 Output: 124 Input: x = 124422 Output: 1242 Input: x = 11332 Output: 132
In the given problem, we will loop through all the numbers and remove the duplicates.
In the given method we will iterate through all the numbers now n numbers from right to left. We iterate through the numbers of n by taking n modulo 10 and then dividing n by 10. Now our current number is n mod 10. We check it against the previous number. If the numbers are equal, we now iterate over n. If they are not similar, we add this number to the new number, change the previous number to the current number, and continue the loop.
#include <bits/stdc++.h> #define MOD 1000000007 using namespace std; int main() { int n = 1222333232; // given n int new_n = 0; // new number int po = 1; // will b multiple of ten for new digits int prev = -1; // previous digit int curr; // current digit while(n) { curr = n % 10; if(prev != curr) { // if a digit is not repeated then we go in this block new_n = new_n + (curr * po); // we add a new digit to new_n po *= 10; prev = curr; } n /= 10; } cout << new_n << "\n"; return 0; }
123232
In the above method, we simply iterate through the numbers of n, when our previous When a number does not match the current number we add such number to our new number and as the number is added we also add po which is used for the position of our number if our current and previous number Match - We don't run this block of code and continue looping until n becomes 0.
In this article, we solved the problem of removing duplicate digits from a given number. We also learned the C program for this problem and our complete method of solving this problem (normal method). We can write the same program in other languages like C, Java, Python and others. Hope you find this article helpful.
The above is the detailed content of Remove duplicate digits from given number using C++. For more information, please follow other related articles on the PHP Chinese website!