Open In App

Print all repeating digits present in a given number in sorted order

Last Updated : 06 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to print all the repeating digits present in N in sorted order.

Examples:

Input: N = 45244336543
Output: 3 4 5
Explanation: The duplicate digits are 3 4 5

Input: N = 987065467809
Output: 0 6 7 8 9

Approach: The idea is to use Hashing to store the frequency of digits of N and print those digits whose frequency exceeds 1.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print repeating
// digits present in the number N
void printDup(string N)
{
  // Stores the count of
  // unique digits
  int res = 0;
 
  // Map to store frequency
  // of each digit
  int cnt[10];
 
  // Set count of all digits to 0
  for (int i = 0; i < 10; i++)
    cnt[i] = 0;
 
  // Traversing the string
  for (int i = 0; i < N.size(); i++) {
 
    // Convert the character
    // to equivalent integer
    int digit = (N[i] - '0');
 
    // Increase the count of digit
    cnt[digit] += 1;
  }
 
  // Traverse the Map
  for (int i = 0; i < 10; i++) {
 
    // If frequency
    // of digit exceeds 1
    if (cnt[i] > 1)
      cout << i << " ";
  }
 
  cout << endl;
}
 
// Driver Code
int main()
{
 
  string N = "45244336543";
 
  // Function call to print
  // repeating digits in N
  printDup(N);
 
  return 0;
}
 
// This code is contributed by Kingash.


Java




// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
  // Function to print repeating
  // digits present in the number N
  static void printDup(String N)
  {
    // Stores the count of
    // unique digits
    int res = 0;
 
    // Map to store frequency
    // of each digit
    int cnt[] = new int[10];
 
    // Traversing the string
    for (int i = 0; i < N.length(); i++) {
 
      // Convert the character
      // to equivalent integer
      int digit = (N.charAt(i) - '0');
 
      // Increase the count of digit
      cnt[digit] += 1;
    }
 
    // Traverse the Map
    for (int i = 0; i < 10; i++) {
 
      // If frequency
      // of digit exceeds 1
      if (cnt[i] > 1)
        System.out.print(i + " ");
    }
 
    System.out.println();
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    String N = "45244336543";
 
    // Function call to print
    // repeating digits in N
    printDup(N);
  }
}
 
// This code is contributed by Kingash.


Python3




# Python implementation
# of the above approach
 
# Function to print repeating
# digits present in the number N
def printDup(N):
 
    # Stores the count of
    # unique digits
    res = 0
 
    # Set count of all digits to 0
    cnt = [0] * 10
 
    # Convert integer to
    # equivalent string
    string = str(N)
 
    # Traversing the string
    for i in string:
 
        # Convert the character
        # to equivalent integer
        digit = int(i)
 
        # Increase the count of digit
        cnt[digit] += 1
 
    # Traverse the Map
    for i in range(10):
 
        # If frequency
        # of digit is 1
        if (cnt[i] > 1):
 
            # If count exceeds 1
            print(i, end=" ")
 
 
# Driver Code
 
N = 45244336543
 
# Function call to print
# repeating digits in N
printDup(N)


C#




// C# program to implement
// the above approach
using System;
 
class GFG
{
 
  // Function to print repeating
  // digits present in the number N
  static void printDup(string N)
  {
     
    // Stores the count of
    // unique digits
 
    // Map to store frequency
    // of each digit
    int[] cnt = new int[10];
 
    // Traversing the string
    for (int i = 0; i < N.Length; i++) {
 
      // Convert the character
      // to equivalent integer
      int digit = (N[i] - '0');
 
      // Increase the count of digit
      cnt[digit] += 1;
    }
 
    // Traverse the Map
    for (int i = 0; i < 10; i++) {
 
      // If frequency
      // of digit exceeds 1
      if (cnt[i] > 1)
        Console.Write(i + " ");
    }
 
    Console.WriteLine();
  }
 
  // Driver code
  public static void Main()
  {
 
    string N = "45244336543";
 
    // Function call to print
    // repeating digits in N
    printDup(N);
  }
}
 
// This code is contributed by code_hunt.


Javascript




<script>
// Javascript program for the above approach
 
// Function to print repeating
// digits present in the number N
function printDup(N)
{
  // Stores the count of
  // unique digits
  var res = 0;
 
  // Map to store frequency
  // of each digit
  var cnt = Array(10);
 
  // Set count of all digits to 0
  for (var i = 0; i < 10; i++)
    cnt[i] = 0;
 
  // Traversing the string
  for (var i = 0; i < N.length; i++) {
 
    // Convert the character
    // to equivalent integer
    var digit = (N[i] - '0');
 
    // Increase the count of digit
    cnt[digit] += 1;
  }
 
  // Traverse the Map
  for (var i = 0; i < 10; i++) {
 
    // If frequency
    // of digit exceeds 1
    if (cnt[i] > 1)
      document.write( i + " ");
  }
 
  document.write("<br>");
}
 
// Driver Code
var N = "45244336543";
// Function call to print
// repeating digits in N
printDup(N);
 
</script>


Output

3 4 5 

Time Complexity: O(N)
Auxiliary Space: O(1)

Approach 2: Using count().
Using count() we try to find the character whose occurrence is more than once, store it in a data structure, and sort it.

C++




#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_set>
 
using namespace std;
 
int main()
{
    // initializing number
    long long N = 45244336543;
    unordered_set<char> x;
    string s = to_string(N);
   
      // using count method
    for (char i : s)
    {
        if (count(s.begin(), s.end(), i) > 1 && x.find(i) == x.end())
        {
            x.insert(i);
        }
    }
    string result;
    for (char i : x)
    {
        result += i;
    }
    sort(result.begin(), result.end());
    cout << result << endl;
    return 0;
}
 
// This code is contributed by phasing17


Java




// Java code to implement the approach
 
import java.util.*;
import java.util.stream.*;
 
class GFG {
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Initializing number
        long N = 45244336543L;
 
        // Creating hashset to store digits
        HashSet<Character> x = new HashSet<Character>();
 
        // Converting number to string
        String s = Long.toString(N);
 
        // using groupby method
        Map<Character, Long> groups
            = s.chars()
                  .mapToObj(c -> (char)c)
                  .collect(Collectors.groupingBy(
                      c -> c, Collectors.counting()));
        for (Map.Entry<Character, Long> group :
             groups.entrySet()) {
            if (group.getValue() > 1
                && !x.contains(group.getKey())) {
                x.add(group.getKey());
            }
        }
 
        String result = x.stream()
                            .sorted()
                            .map(Object::toString)
                            .collect(Collectors.joining());
        for (char r : result.toCharArray())
            System.out.print(r + " ");
    }
}
// This code is contributed by phasing17


Python3




# Python implementation
# of the above approach
 
#initializing number
N = 45244336543
x=[]
s=str(N)
for i in s:
    if(s.count(i)>1 and i not in x):
        x.append(i)
x.sort()
print(' '.join(x))


C#




using System;
using System.Linq;
using System.Collections.Generic;
 
class GFG {
  static void Main(string[] args) {
    long N = 45244336543;
    HashSet<char> x = new HashSet<char>();
    var s = N.ToString();
 
    // using groupby method
    var groups = s.GroupBy(c => c);
    foreach (var group in groups)
    {
      if (group.Count() > 1 && !x.Contains(group.Key))
      {
        x.Add(group.Key);
      }
    }
 
    var result = string.Concat(x.OrderBy(c => c));
    Console.WriteLine(result);
  }
 
}
 
// This code is contributed by phasing17


Javascript




// JavaScript implementation
// of the above approach
 
// Initializing number
const N = 45244336543;
let x = new Set();
const s = String(N);
for (let i of s)
  if (s.split(i).length > 2)
    x.add(i);
   
x = Array.from(x)
x.sort(function(a, b)
{
    return a - b;
});
 
console.log(x.join(' '));
 
// This code is contributed by phasing17.


Output

3 4 5

Time Complexity: O(n log n)
Auxiliary Space: O(n)
 

Approach 3: Using the Counter() function

Python3




# Python implementation
# of the above approach
from collections import Counter
# initializing number
N = 45244336543
x = []
s = str(N)
freq = Counter(s)
for key, value in freq.items():
    if value > 1:
        x.append(key)
x.sort()
print(' '.join(x))


Time Complexity: O(n log n)
Auxiliary Space: O(n)
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads