Remove recurring digits in a given number
Last Updated :
13 Apr, 2023
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 recommend you to minimize your browser and try this yourself first
This problem is similar to Run Length Encoding.
Let num[] be input number represented as character array
1) Initialize index of modified string 'j' as 0.
2) Traverse input string and do following for every digit num[i].
a) Copy current character 'num[i]' to 'num[j]' and increment i & j.
b) Keep incrementing i while num[i] is same as previous digit.
3) Add string termination character at 'num[j]'
Below is the implementation of above algorithm.
C++
#include <bits/stdc++.h>
using namespace std;
void removeRecurringDigits( char num[])
{
int len = strlen (num);
int j = 0;
for ( int i=0; i<len; i++)
{
num[j++] = num[i];
while (i + 1 < len && num[i] == num[i+1])
i++;
}
num[j] = '\0' ;
}
int main()
{
char num[] = "1299888833" ;
removeRecurringDigits(num);
cout << "Modified number is " << num;
return 0;
}
|
Java
class GFG
{
static String removeRecurringDigits( char num[])
{
int len = num.length;
int j = 0 ;
String s = "" ;
for ( int i = 0 ; i < len; i++)
{
s += String.valueOf(num[i]);
while (i + 1 < len && num[i] == num[i + 1 ])
{
i++;
}
}
return s;
}
public static void main(String[] args)
{
char num[] = "1299888833" .toCharArray();
System.out.print( "Modified number is " +
removeRecurringDigits(num));
}
}
|
Python3
def removeRecurringDigits(num):
l = len (num)
(i, j) = ( 0 , 0 )
str = ''
while i < l:
str + = num[i]
j + = 1
while (i + 1 < l and num[i] = = num[i + 1 ]):
i + = 1
i + = 1
return str
if __name__ = = '__main__' :
num = '1299888833'
print ( 'Modified number is {}' . format (
removeRecurringDigits(num)))
|
C#
using System;
class GFG
{
static String removeRecurringDigits( char []num)
{
int len = num.Length;
int j = 0;
String s = "" ;
for ( int i = 0; i < len; i++)
{
s += String.Join( "" ,num[i]);
while (i + 1 < len && num[i] == num[i + 1])
{
i++;
}
}
return s;
}
public static void Main()
{
char []num = "1299888833" .ToCharArray();
Console.Write( "Modified number is " +
removeRecurringDigits(num));
}
}
|
Javascript
<script>
function removeRecurringDigits(num)
{
let len = num.length;
let j = 0;
let s = "" ;
for (let i = 0; i < len; i++)
{
s += (num[i]);
while (i + 1 < len && num[i] == num[i + 1])
{
i++;
}
}
return s;
}
let num= "1299888833" .split( "" );
document.write( "Modified number is " +
removeRecurringDigits(num));
</script>
|
Output
Modified number is 12983
Time complexity: O(N2) where N is the length of the string.
Auxiliary space complexity: O(1).
Method 2:
The main idea behind this approach is that we will traverse the string check next character of all index from 0 to n-2.
If the value at both indexes is the same then we will delete anyone of them. so, the length of the string will get reduced by 1.
Below is the implementation of the above approach.
C++
#include<bits/stdc++.h>
using namespace std;
void updatestring(string s){
int x=s.length()-2;
for ( int i=0;i<x;i++){
if (i>s.length()-2){
break ;
}
if (s[i]==s[i+1]){
s.erase(s.begin()+i);
i--;
}
}
cout<< "number without recurring digits: " <<s<<endl;
}
int main() {
string s= "1299888833222" ;
updatestring(s);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void updatestring(String s) {
int x = s.length() - 2 ;
for ( int i = 0 ; i < x; i++) {
if (i > s.length() - 2 ) {
break ;
}
if (s.charAt(i) == s.charAt(i + 1 )) {
s = s.substring( 0 , i) + s.substring(i + 1 );
i--;
}
}
System.out.println( "number without recurring digits: " + s);
}
public static void main(String[] args) {
String s = "1299888833222" ;
updatestring(s);
}
}
|
Python3
def removeRecurringDigits(s):
x = len (s) - 2
for i in range (x):
if (i > len (s) - 2 ):
break
if (s[i] = = s[i + 1 ]):
s = s[:i] + s[i + 1 :]
i = i - 1
return s
if __name__ = = '__main__' :
num = '1299888833'
print ( 'Modified number is {}' . format (
removeRecurringDigits(num)))
|
C#
using System;
class GFG
{
static string updatestring( string s){
int x = s.Length - 2;
for ( int i = 0; i<x; i++){
if (i > s.Length - 2){
break ;
}
if (s[i] == s[i+1])
{
s = s.Substring(0, i) + s.Substring(i+1);
i -= 1;
}
}
return s;
}
public static void Main()
{
string num = "1299888833222" ;
Console.Write( "number without recurring degits: " +updatestring(num));
}
}
|
Javascript
function removeRecurringDigits(s){
let x = s.length - 2;
for (let i = 0; i<x; i++){
if (i > s.length - 2)
break ;
if (s[i] == s[i+1]){
s = s.substr(0, i+1) + s.substr(i+2, s.length);
i = i-1;
}
}
return s;
}
let num = "1299888833222" ;
document.write( "Modified number is : " + removeRecurringDigits(num));
|
Output
number without recurring digits: 129832
Time complexity: O(N) where N is the length of the string.
Auxiliary space complexity: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...