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

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 5Input: 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.
- Initialize a HashMap, to store the frequency of digits 0-9.
- Convert the integer to its equivalent string.
- Iterate over the characters of the string, and for each character, perform the following steps:
- Convert the current character to an integer using typecasting
- Increment the count of that digit in a HashMap.
- Traverse the HashMap and print the digits whose count 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 Nvoid 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 Codeint 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 approachimport 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 Ndef 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 NprintDup(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 Nfunction 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 Codevar N = "45244336543";// Function call to print// repeating digits in NprintDup(N);Â
</script> |
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 numberN = 45244336543x=[]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 numberconst 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. |
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 approachfrom collections import Counter# initializing numberN = 45244336543x = []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)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



