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 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to remove recurring digits from
// a given number
#include <bits/stdc++.h>
using namespace std;
  
/* Removes recurring digits in num[]  */
void removeRecurringDigits(char num[])
{
    int len = strlen(num);
  
    int j = 0; // Index in modified string
  
    /* Traverse digits of given number one by one */
    for (int i=0; i<len; i++)
    {
        /* Copy the first occurrence of new digit */
        num[j++] = num[i];
  
        /* Remove repeating occurrences of digit */
        while (i + 1 < len && num[i] == num[i+1])
            i++;
    }
  
    /* terminate the modified string */
    num[j] = '\0';
}
  
/* Driver program to test above function */
int main()
{
    char num[] = "1299888833";
    removeRecurringDigits(num);
    cout << "Modified number is " << num;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to remove recurring 
// digits from a given number
class GFG 
{
  
    /* Removes recurring digits in num[] */
    static String removeRecurringDigits(char num[])
    {
        int len = num.length;
  
        int j = 0; // Index in modified string
        String s = "";
          
        /* Traverse digits of given number one by one */
        for (int i = 0; i < len; i++) 
        {
              
            /* Copy the first occurrence of new digit */
            s += String.valueOf(num[i]);
  
            /* Remove repeating occurrences of digit */
            while (i + 1 < len && num[i] == num[i + 1]) 
            {
                i++;
            }
        }
        return s;
    }
  
    /* Driver code */
    public static void main(String[] args) 
    {
        char num[] = "1299888833".toCharArray();
        System.out.print("Modified number is "
                        removeRecurringDigits(num));
    }
}
  
// This code has been contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to remove recurring 
// digits from a given number
using System;
  
class GFG 
{
  
    /* Removes recurring digits in num[] */
    static String removeRecurringDigits(char []num)
    {
        int len = num.Length;
  
        int j = 0; // Index in modified string
        String s = "";
          
        /* Traverse digits of given number one by one */
        for (int i = 0; i < len; i++) 
        {
              
            /* Copy the first occurrence of new digit */
            s += String.Join("",num[i]);
  
            /* Remove repeating occurrences of digit */
            while (i + 1 < len && num[i] == num[i + 1]) 
            {
                i++;
            }
        }
        return s;
    }
  
    /* Driver code */
    public static void Main() 
    {
        char []num = "1299888833".ToCharArray();
        Console.Write("Modified number is "
                        removeRecurringDigits(num));
    }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


Output:

Modified number is 12983

This article is contributed by Priyanka. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up

Improved By : 29AjayKumar, princiraj1992



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.