Print all repeating digits present in a given number in sorted order
Last Updated :
06 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void printDup(string N)
{
int res = 0;
int cnt[10];
for ( int i = 0; i < 10; i++)
cnt[i] = 0;
for ( int i = 0; i < N.size(); i++) {
int digit = (N[i] - '0' );
cnt[digit] += 1;
}
for ( int i = 0; i < 10; i++) {
if (cnt[i] > 1)
cout << i << " " ;
}
cout << endl;
}
int main()
{
string N = "45244336543" ;
printDup(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void printDup(String N)
{
int res = 0 ;
int cnt[] = new int [ 10 ];
for ( int i = 0 ; i < N.length(); i++) {
int digit = (N.charAt(i) - '0' );
cnt[digit] += 1 ;
}
for ( int i = 0 ; i < 10 ; i++) {
if (cnt[i] > 1 )
System.out.print(i + " " );
}
System.out.println();
}
public static void main(String[] args)
{
String N = "45244336543" ;
printDup(N);
}
}
|
Python3
def printDup(N):
res = 0
cnt = [ 0 ] * 10
string = str (N)
for i in string:
digit = int (i)
cnt[digit] + = 1
for i in range ( 10 ):
if (cnt[i] > 1 ):
print (i, end = " " )
N = 45244336543
printDup(N)
|
C#
using System;
class GFG
{
static void printDup( string N)
{
int [] cnt = new int [10];
for ( int i = 0; i < N.Length; i++) {
int digit = (N[i] - '0' );
cnt[digit] += 1;
}
for ( int i = 0; i < 10; i++) {
if (cnt[i] > 1)
Console.Write(i + " " );
}
Console.WriteLine();
}
public static void Main()
{
string N = "45244336543" ;
printDup(N);
}
}
|
Javascript
<script>
function printDup(N)
{
var res = 0;
var cnt = Array(10);
for ( var i = 0; i < 10; i++)
cnt[i] = 0;
for ( var i = 0; i < N.length; i++) {
var digit = (N[i] - '0' );
cnt[digit] += 1;
}
for ( var i = 0; i < 10; i++) {
if (cnt[i] > 1)
document.write( i + " " );
}
document.write( "<br>" );
}
var N = "45244336543" ;
printDup(N);
</script>
|
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()
{
long long N = 45244336543;
unordered_set< char > x;
string s = to_string(N);
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;
}
|
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
long N = 45244336543L;
HashSet<Character> x = new HashSet<Character>();
String s = Long.toString(N);
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 + " " );
}
}
|
Python3
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();
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);
}
}
|
Javascript
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( ' ' ));
|
Time Complexity: O(n log n)
Auxiliary Space: O(n)
Approach 3: Using the Counter() function
Python3
from collections import Counter
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)
Share your thoughts in the comments
Please Login to comment...