Count of repeating digits in a given Number
Last Updated :
21 Sep, 2023
Given a number N, the task is to count the total number of repeating digits in the given number.
Examples:
Input: N = 99677
Output: 2
Explanation:
In the given number only 9 and 7 are repeating, hence the answer is 2.
Input: N = 12
Output: 0
Explanation:
In the given number no digits are repeating, hence the answer is 0.
Naive Approach: The idea is to use two nested loops. In the first loop, traverse from the first digit of the number to the last, one by one. Then for each digit in the first loop, run a second loop and search if this digit is present anywhere else as well in the number. If yes, then increase the required count by 1. In the end, print the calculated count.
Algorithm
Define a function called count_repeating_digits(num)
Convert the input number to a string using the to_string() function
Store it in a variable called str_num
Initialize a variable called count to 0
Use a nested loop to compare each digit in the string with every other digit
a. For each digit i in str_num, start another loop to compare it with all the digits j following it
b. If a repeating digit is found, increment the count by 1 and exit the inner loop
c. Continue comparing the rest of the digits
Return the count of repeating digits
Call the count_repeating_digits(num) function and store the result in a variable called repeating_digits
Display the number of repeating digits on the console
C++
#include <iostream>
#include <string>
using namespace std;
int count_repeating_digits( int num) {
int count = 0;
string str_num = to_string(num);
for ( int i = 0; i < str_num.length(); i++) {
for ( int j = i+1; j < str_num.length(); j++) {
if (str_num[i] == str_num[j]) {
count++;
break ;
}
}
}
return count;
}
int main() {
int num=99677;
int repeating_digits = count_repeating_digits(num);
cout << "Number of repeating digits: " << repeating_digits << endl;
return 0;
}
|
Java
public class GFG {
public static int countRepeatingDigits( int num) {
int count = 0 ;
String strNum = Integer.toString(num);
for ( int i = 0 ; i < strNum.length(); i++) {
for ( int j = i + 1 ; j < strNum.length(); j++) {
if (strNum.charAt(i) == strNum.charAt(j)) {
count++;
break ;
}
}
}
return count;
}
public static void main(String[] args) {
int num = 99677 ;
int repeatingDigits = countRepeatingDigits(num);
System.out.println( "Number of repeating digits: " + repeatingDigits);
}
}
|
Python3
def count_repeating_digits(num):
count = 0
str_num = str (num)
for i in range ( len (str_num)):
for j in range (i + 1 , len (str_num)):
if str_num[i] = = str_num[j]:
count + = 1
break
return count
num = 99677
repeating_digits = count_repeating_digits(num)
print ( "Number of repeating digits:" , repeating_digits)
|
C#
using System;
class GFG
{
public static int CountRepeatingDigits( int num)
{
int count = 0;
string strNum = num.ToString();
for ( int i = 0; i < strNum.Length; i++)
{
for ( int j = i + 1; j < strNum.Length; j++)
{
if (strNum[i] == strNum[j])
{
count++;
break ;
}
}
}
return count;
}
public static void Main( string [] args)
{
int num = 99677;
int repeatingDigits = CountRepeatingDigits(num);
Console.WriteLine( "Number of repeating digits: " + repeatingDigits);
}
}
|
Javascript
function count_repeating_digits(num) {
let count = 0;
let str_num = num.toString();
for (let i = 0; i < str_num.length; i++) {
for (let j = i+1; j < str_num.length; j++) {
if (str_num[i] == str_num[j]) {
count++;
break ;
}
}
}
return count;
}
let num=99677;
let repeating_digits = count_repeating_digits(num);
document.write( "Number of repeating digits: " + repeating_digits);
|
Output
Number of repeating digits: 2
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The idea is to use Hashing to store the frequency of the digits and then count the digits with a frequency equal to more than 1. Follow the steps below to solve the problem:
- Create an array of size 10 to store the count of digits 0 – 9. Initially store each index as 0.
- Now for each digit of number N, increment the count of that index in the array.
- Traverse the array and count the indices that have value more than 1.
- In the end, print this count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countRepeatingDigits( int N)
{
int res = 0;
int cnt[10] = { 0 };
while (N > 0) {
int rem = N % 10;
cnt[rem]++;
N = N / 10;
}
for ( int i = 0; i < 10; i++) {
if (cnt[i] > 1) {
res++;
}
}
return res;
}
int main()
{
int N = 12;
cout << countRepeatingDigits(N);
return 0;
}
|
Java
class GFG{
static int countRepeatingDigits( int N)
{
int res = 0 ;
int cnt[] = new int [ 10 ];
while (N > 0 )
{
int rem = N % 10 ;
cnt[rem]++;
N = N / 10 ;
}
for ( int i = 0 ; i < 10 ; i++)
{
if (cnt[i] > 1 )
{
res++;
}
}
return res;
}
public static void main(String[] args)
{
int N = 12 ;
System.out.println(countRepeatingDigits(N));
}
}
|
Python3
def countRepeatingDigits(N):
res = 0
cnt = [ 0 ] * 10
while (N > 0 ):
rem = N % 10
cnt[rem] + = 1
N = N / / 10
for i in range ( 10 ):
if (cnt[i] > 1 ):
res + = 1
return res
N = 12
print (countRepeatingDigits(N))
|
C#
using System;
class GFG{
static int countRepeatingDigits( int N)
{
int res = 0;
int []cnt = new int [10];
while (N > 0)
{
int rem = N % 10;
cnt[rem]++;
N = N / 10;
}
for ( int i = 0; i < 10; i++)
{
if (cnt[i] > 1)
{
res++;
}
}
return res;
}
public static void Main(String[] args)
{
int N = 12;
Console.WriteLine(countRepeatingDigits(N));
}
}
|
Javascript
<script>
function countRepeatingDigits(N)
{
var res = 0;
var cnt = Array(10).fill(0);
while (N > 0) {
var rem = N % 10;
cnt[rem]++;
N = N / 10;
}
for ( var i = 0; i < 10; i++) {
if (cnt[i] > 1) {
res++;
}
}
return res;
}
var N = 12;
document.write( countRepeatingDigits(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Method #2:Using built in python functions:
- Convert integer to string.
- Use the Counter function to count the frequency of characters.
- If the frequency is greater than 1 increment the count
Below is the implementation:
C++
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
int countRepeatingDigits( int N)
{
string number = to_string(N);
int count = 0;
unordered_map< char , int > frequency;
for ( char c : number) {
frequency++;
}
for ( auto & it : frequency) {
if (it.second > 1) {
count++;
}
}
return count;
}
int main()
{
int N = 1232145;
cout << countRepeatingDigits(N) << endl;
return 0;
}
|
Java
import java.util.HashMap;
public class Main {
static int countRepeatingDigits( int N)
{
String number = Integer.toString(N);
int count = 0 ;
HashMap<Character, Integer> frequency
= new HashMap<>();
for ( char c : number.toCharArray()) {
frequency.put(c,
frequency.getOrDefault(c, 0 ) + 1 );
}
for ( char key : frequency.keySet()) {
if (frequency.get(key) > 1 ) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int N = 1232145 ;
System.out.println(countRepeatingDigits(N));
}
}
|
Python3
from collections import Counter
def countRepeatingDigits(N):
number = str (N)
count = 0
frequency = Counter(number)
for i in frequency:
if (frequency[i] > 1 ):
count = count + 1
return count
N = 1232145
print (countRepeatingDigits(N))
|
C#
using System;
using System.Collections.Generic;
public class Program
{
public static int countRepeatingDigits( int N)
{
string number = N.ToString();
int count = 0;
Dictionary< char , int > frequency = new Dictionary< char , int >();
foreach ( char c in number)
{
if (!frequency.ContainsKey(c))
{
frequency = 0;
}
frequency++;
}
foreach (KeyValuePair< char , int > item in frequency)
{
if (item.Value > 1)
{
count++;
}
}
return count;
}
public static void Main()
{
int N = 1232145;
Console.WriteLine(countRepeatingDigits(N));
}
}
|
Javascript
function countRepeatingDigits(N)
{
let number = N.toString();
let count = 0;
let frequency = {};
for (let c of number) {
frequency = (frequency || 0) + 1;
}
for (let key in frequency) {
if (frequency[key] > 1) {
count++;
}
}
return count;
}
let N = 1232145;
console.log(countRepeatingDigits(N));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...