# Remove repeated digits in a given number

Given an integer number, the task is to remove any consecutive repeated digits from it and return the modified number. If the number is negative, the sign should be retained in the output. For instance, if the input number is -123300, the output should be -1230.

## Example

Let's consider an example to understand the problem better:

Input: 1134332

Output: 13432

## Pseudocode

``````Procedure removeRecurring(x):
num = x
if x < 0:
num = -num
back = num % 10
current = 0
ans = back
mul = 10
num = num / 10
while num > 0:
current = num % 10
if current != back:
ans = (current * mul) + ans
back = current
mul *= 10
num /= 10
if x < 0:
ans = -ans
Display "Given number:", x
Display "Output:", ans
``````

## Algorithm Explanation

The algorithm takes an input integer x and initializes a variable num with the absolute value of x. If x is negative, num will hold its positive counterpart. The algorithm then extracts the last digit of num and stores it in back. This digit will serve as a reference for comparison.

The algorithm also initializes the variable ans with the last digit, as the final result will be constructed digit by digit. The variable mul is initialized with 10, which will be used to position the current digit properly in the final result.

Next, the algorithm starts a loop to iterate through the remaining digits of num (excluding the last digit already stored in back). In each iteration, it extracts the next digit, current, and compares it with back. If current is different from back, it means a new digit has been found, and it needs to be added to the result. The algorithm places current at the beginning of ans and updates back to the value of current. It also adjusts the value of mul for the next digit position.

This process continues until all digits have been processed. After the loop ends, the algorithm checks if the original input x was negative. If so, it makes ans negative as well to match the sign of the input.

Finally, the algorithm displays the original input x and the modified number ans as the output.

## Code Solution

Here given code implementation process.

``````/*
C Program for
Remove repeated digits in a given number
*/
#include <stdio.h>

// Remove recurring digits in a given number
void removeRecurring(int x)
{
int num = x;
if (x < 0)
{
// When given number is negative
num = -num;
}
// Define some auxiliary variables
int back = num % 10;
int current = 0;
int ans = back;
int mul = 10;
num = num / 10;
while (num > 0)
{
current = num % 10;
if (current != back)
{
// Add current digit at beginning of result
ans = (current *mul) + ans;
back = current;
mul *= 10;
}
// reduce number
num /= 10;
}
if (x < 0)
{
ans = -ans;
}
// Display result
printf("\n Given number : %d", x);
printf("\n Output       : %d", ans);
}
int main()
{
// Test
removeRecurring(1134332);
removeRecurring(-123300);
return 0;
}``````

#### Output

`````` Given number : 1134332
Output       : 13432
Given number : -123300
Output       : -1230``````
``````/*
Java Program for
Remove repeated digits in a given number
*/
class Recurring
{
// Remove recurring digits in a given number
public void removeRecurring(int x)
{
int num = x;
if (x < 0)
{
// When given number is negative
num = -num;
}
// Define some auxiliary variables
int back = num % 10;
int current = 0;
int ans = back;
int mul = 10;
num = num / 10;
while (num > 0)
{
current = num % 10;
if (current != back)
{
// Add current digit at beginning of result
ans = (current * mul) + ans;
back = current;
mul *= 10;
}
// reduce number
num /= 10;
}
if (x < 0)
{
ans = -ans;
}
// Display result
System.out.print("\n Given number : " + x);
System.out.print("\n Output : " + ans);
}
public static void main(String[] args)
{
// Test
}
}``````

#### Output

`````` Given number : 1134332
Output : 13432
Given number : -123300
Output : -1230``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program for
Remove repeated digits in a given number
*/
class Recurring
{
public:
// Remove recurring digits in a given number
void removeRecurring(int x)
{
int num = x;
if (x < 0)
{
// When given number is negative
num = -num;
}
// Define some auxiliary variables
int back = num % 10;
int current = 0;
int ans = back;
int mul = 10;
num = num / 10;
while (num > 0)
{
current = num % 10;
if (current != back)
{
// Add current digit at beginning of result
ans = (current *mul) + ans;
back = current;
mul *= 10;
}
// reduce number
num /= 10;
}
if (x < 0)
{
ans = -ans;
}
// Display result
cout << "\n Given number : " << x;
cout << "\n Output : " << ans;
}
};
int main()
{
// Test
return 0;
}``````

#### Output

`````` Given number : 1134332
Output : 13432
Given number : -123300
Output : -1230``````
``````// Include namespace system
using System;
/*
C# Program for
Remove repeated digits in a given number
*/
public class Recurring
{
// Remove recurring digits in a given number
public void removeRecurring(int x)
{
int num = x;
if (x < 0)
{
// When given number is negative
num = -num;
}
// Define some auxiliary variables
int back = num % 10;
int current = 0;
int ans = back;
int mul = 10;
num = num / 10;
while (num > 0)
{
current = num % 10;
if (current != back)
{
// Add current digit at beginning of result
ans = (current * mul) + ans;
back = current;
mul *= 10;
}
// reduce number
num /= 10;
}
if (x < 0)
{
ans = -ans;
}
// Display result
Console.Write("\n Given number : " + x);
Console.Write("\n Output : " + ans);
}
public static void Main(String[] args)
{
// Test
}
}``````

#### Output

`````` Given number : 1134332
Output : 13432
Given number : -123300
Output : -1230``````
``````<?php
/*
Php Program for
Remove repeated digits in a given number
*/
class Recurring
{
// Remove recurring digits in a given number
public	function removeRecurring(\$x)
{
\$num = \$x;
if (\$x < 0)
{
// When given number is negative
\$num = -\$num;
}
// Define some auxiliary variables
\$back = \$num % 10;
\$current = 0;
\$ans = \$back;
\$mul = 10;
\$num = intval(\$num / 10);
while (\$num > 0)
{
\$current = \$num % 10;
if (\$current != \$back)
{
// Add current digit at beginning of result
\$ans = (\$current * \$mul) + \$ans;
\$back = \$current;
\$mul *= 10;
}
// reduce number
\$num = intval(\$num / 10);
}
if (\$x < 0)
{
\$ans = -\$ans;
}
// Display result
echo "\n Given number : ". \$x;
echo "\n Output : ". \$ans;
}
}

function main()
{
// Test
}
main();``````

#### Output

`````` Given number : 1134332
Output : 13432
Given number : -123300
Output : -1230``````
``````/*
Node Js Program for
Remove repeated digits in a given number
*/
class Recurring
{
// Remove recurring digits in a given number
removeRecurring(x)
{
var num = x;
if (x < 0)
{
// When given number is negative
num = -num;
}
// Define some auxiliary variables
var back = num % 10;
var current = 0;
var ans = back;
var mul = 10;
num = parseInt(num / 10);
while (num > 0)
{
current = num % 10;
if (current != back)
{
// Add current digit at beginning of result
ans = (current * mul) + ans;
back = current;
mul *= 10;
}
// reduce number
num = parseInt(num / 10);
}
if (x < 0)
{
ans = -ans;
}
// Display result
process.stdout.write("\n Given number : " + x);
process.stdout.write("\n Output : " + ans);
}
}

function main()
{
// Test
}
main();``````

#### Output

`````` Given number : 1134332
Output : 13432
Given number : -123300
Output : -1230``````
``````#   Python 3 Program for
#   Remove repeated digits in a given number

class Recurring :
#  Remove recurring digits in a given number
def removeRecurring(self, x) :
num = x
if (x < 0) :
#  When given number is negative
num = -num

#  Define some auxiliary variables
back = num % 10
current = 0
ans = back
mul = 10
num = int(num / 10)
while (num > 0) :
current = num % 10
if (current != back) :
#  Add current digit at beginning of result
ans = (current * mul) + ans
back = current
mul *= 10

num = int(num /
#  reduce number
10)

if (x < 0) :
ans = -ans

#  Display result
print("\n Given number : ", x, end = "")
print("\n Output : ", ans, end = "")

def main() :
#  Test

if __name__ == "__main__": main()``````

#### Output

`````` Given number :  1134332
Output :  13432
Given number :  -123300
Output :  -1230``````
``````#   Ruby Program for
#   Remove repeated digits in a given number

class Recurring
#  Remove recurring digits in a given number
def removeRecurring(x)
num = x
if (x < 0)
#  When given number is negative
num = -num
end

#  Define some auxiliary variables
back = num % 10
current = 0
ans = back
mul = 10
num = num / 10
while (num > 0)
current = num % 10
if (current != back)
#  Add current digit at beginning of result
ans = (current * mul) + ans
back = current
mul *= 10
end

#  reduce number
num /= 10
end

if (x < 0)
ans = -ans
end

#  Display result
print("\n Given number : ", x)
print("\n Output : ", ans)
end

end

def main()
#  Test
end

main()``````

#### Output

`````` Given number : 1134332
Output : 13432
Given number : -123300
Output : -1230``````
``````/*
Scala Program for
Remove repeated digits in a given number
*/
class Recurring
{
// Remove recurring digits in a given number
def removeRecurring(x: Int): Unit = {
var num: Int = x;
if (x < 0)
{
// When given number is negative
num = -num;
}
// Define some auxiliary variables
var back: Int = num % 10;
var current: Int = 0;
var ans: Int = back;
var mul: Int = 10;
num = (num / 10).toInt;
while (num > 0)
{
current = num % 10;
if (current != back)
{
// Add current digit at beginning of result
ans = (current * mul) + ans;
back = current;
mul *= 10;
}
// reduce number
num = (num / 10).toInt;
}
if (x < 0)
{
ans = -ans;
}
// Display result
print("\n Given number : " + x);
print("\n Output : " + ans);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Recurring = new Recurring();
// Test
}
}``````

#### Output

`````` Given number : 1134332
Output : 13432
Given number : -123300
Output : -1230``````
``````/*
Swift 4 Program for
Remove repeated digits in a given number
*/
class Recurring
{
// Remove recurring digits in a given number
func removeRecurring(_ x: Int)
{
var num: Int = x;
if (x < 0)
{
// When given number is negative
num = -num;
}
// Define some auxiliary variables
var back: Int = num % 10;
var current: Int = 0;
var ans: Int = back;
var mul: Int = 10;
num = num / 10;
while (num > 0)
{
current = num % 10;
if (current  != back)
{
// Add current digit at beginning of result
ans = (current * mul) + ans;
back = current;
mul *= 10;
}
// reduce number
num /= 10;
}
if (x < 0)
{
ans = -ans;
}
// Display result
print("\n Given number : ", x, terminator: "");
print("\n Output : ", ans, terminator: "");
}
}
func main()
{
// Test
}
main();``````

#### Output

`````` Given number :  1134332
Output :  13432
Given number :  -123300
Output :  -1230``````
``````/*
Kotlin Program for
Remove repeated digits in a given number
*/
class Recurring
{
// Remove recurring digits in a given number
fun removeRecurring(x: Int): Unit
{
var num: Int = x;
if (x < 0)
{
// When given number is negative
num = -num;
}
// Define some auxiliary variables
var back: Int = num % 10;
var current: Int ;
var ans: Int = back;
var mul: Int = 10;
num = num / 10;
while (num > 0)
{
current = num % 10;
if (current != back)
{
// Add current digit at beginning of result
ans = (current * mul) + ans;
back = current;
mul *= 10;
}
// reduce number
num /= 10;
}
if (x < 0)
{
ans = -ans;
}
// Display result
print("\n Given number : " + x);
print("\n Output : " + ans);
}
}
fun main(args: Array < String > ): Unit
{
// Test
}``````

#### Output

`````` Given number : 1134332
Output : 13432
Given number : -123300
Output : -1230``````

## Time Complexity

Let's analyze the time complexity of the algorithm. The main part of the algorithm involves iterating through the digits of the input number x. The number of digits in x can be represented as O(log10(x)). For each digit, the algorithm performs constant-time operations, so the overall time complexity of the algorithm is O(log10(x)), which can also be approximated as O(log n), where n is the magnitude of the input number.

## Resultant Output Explanation

Let's understand the resultant outputs of the example test cases:

Given number: 1134332

Output: 13432

In this example, the input number is 1134332. The algorithm removes the recurring digits 1 and 3, resulting in the output 13432.

Given number: -123300

Output: -1230

Here, the input number is -123300. The algorithm removes the recurring digit 0, and since the number is negative, the output retains the negative sign, resulting in -1230.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.