Home > Backend Development > C++ > Remove duplicate digits from given number using C++

Remove duplicate digits from given number using C++

WBOY
Release: 2023-09-01 20:17:06
forward
825 people have browsed it

Remove duplicate digits from given number using C++

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
Copy after login

In the given problem, we will loop through all the numbers and remove the duplicates.

Methods to find solution

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.

Example

#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;
}
Copy after login

Output

123232
Copy after login

Explanation of the above code

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.

Conclusion

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template