# Count of repeating digits in a given Number

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.

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

 `// C++ program for the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns the count of ` `// repeating digits of the given number ` `int` `countRepeatingDigits(``int` `N) ` `{ ` `    ``// Initialize a variable to store ` `    ``// count of Repeating digits ` `    ``int` `res = 0; ` ` `  `    ``// Initialize cnt array to ` `    ``// store digit count ` ` `  `    ``int` `cnt = { 0 }; ` ` `  `    ``// Iterate through the digits of N ` `    ``while` `(N > 0) { ` ` `  `        ``// Retrieve the last digit of N ` `        ``int` `rem = N % 10; ` ` `  `        ``// Increase the count of digit ` `        ``cnt[rem]++; ` ` `  `        ``// Remove the last digit of N ` `        ``N = N / 10; ` `    ``} ` ` `  `    ``// Iterate through the cnt array ` `    ``for` `(``int` `i = 0; i < 10; i++) { ` ` `  `        ``// If frequency of digit ` `        ``// is greater than 1 ` `        ``if` `(cnt[i] > 1) { ` ` `  `            ``// Increment the count ` `            ``// of Repeating digits ` `            ``res++; ` `        ``} ` `    ``} ` ` `  `    ``// Return count of repeating digit ` `    ``return` `res; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Given array arr[] ` `    ``int` `N = 12; ` ` `  `    ``// Function Call ` `    ``cout << countRepeatingDigits(N); ` `    ``return` `0; ` `}`

## Java

 `// Java program for the above approach ` `class` `GFG{ ` `   `  `// Function that returns the count of ` `// repeating digits of the given number ` `static` `int` `countRepeatingDigits(``int` `N) ` `{ ` `    ``// Initialize a variable to store ` `    ``// count of Repeating digits ` `    ``int` `res = ``0``; ` `  `  `    ``// Initialize cnt array to ` `    ``// store digit count ` `  `  `    ``int` `cnt[] = ``new` `int``[``10``]; ` `  `  `    ``// Iterate through the digits of N ` `    ``while` `(N > ``0``) ` `    ``{ ` `  `  `        ``// Retrieve the last digit of N ` `        ``int` `rem = N % ``10``; ` `  `  `        ``// Increase the count of digit ` `        ``cnt[rem]++; ` `  `  `        ``// Remove the last digit of N ` `        ``N = N / ``10``; ` `    ``} ` `  `  `    ``// Iterate through the cnt array ` `    ``for` `(``int` `i = ``0``; i < ``10``; i++)  ` `    ``{ ` `  `  `        ``// If frequency of digit ` `        ``// is greater than 1 ` `        ``if` `(cnt[i] > ``1``)  ` `        ``{ ` `  `  `            ``// Increment the count ` `            ``// of Repeating digits ` `            ``res++; ` `        ``} ` `    ``} ` `  `  `    ``// Return count of repeating digit ` `    ``return` `res; ` `} ` `  `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``// Given array arr[] ` `    ``int` `N = ``12``; ` `  `  `    ``// Function Call ` `    ``System.out.println(countRepeatingDigits(N)); ` `} ` `} ` ` `  `// This code is contributed by Ritik Bansal`

## Python3

 `# Python3 program for the above approach  ` ` `  `# Function that returns the count of  ` `# repeating digits of the given number  ` `def` `countRepeatingDigits(N): ` `     `  `    ``# Initialize a variable to store  ` `    ``# count of Repeating digits  ` `    ``res ``=` `0` ` `  `    ``# Initialize cnt array to  ` `    ``# store digit count  ` `    ``cnt ``=` `[``0``] ``*` `10` ` `  `    ``# Iterate through the digits of N  ` `    ``while` `(N > ``0``):  ` ` `  `        ``# Retrieve the last digit of N  ` `        ``rem ``=` `N ``%` `10` ` `  `        ``# Increase the count of digit  ` `        ``cnt[rem] ``+``=` `1` ` `  `        ``# Remove the last digit of N  ` `        ``N ``=` `N ``/``/` `10` `     `  `    ``# Iterate through the cnt array  ` `    ``for` `i ``in` `range``(``10``):  ` ` `  `        ``# If frequency of digit  ` `        ``# is greater than 1  ` `        ``if` `(cnt[i] > ``1``):  ` ` `  `            ``# Increment the count  ` `            ``# of Repeating digits  ` `            ``res ``+``=` `1` `         `  `    ``# Return count of repeating digit  ` `    ``return` `res  ` ` `  `# Driver Code  ` ` `  `# Given array arr[]  ` `N ``=` `12` ` `  `# Function call  ` `print``(countRepeatingDigits(N))  ` ` `  `# This code is contributed by sanjoy_62 `

## C#

 `// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` `  `// Function that returns the count of ` `// repeating digits of the given number ` `static` `int` `countRepeatingDigits(``int` `N) ` `{ ` `    ``// Initialize a variable to store ` `    ``// count of Repeating digits ` `    ``int` `res = 0; ` ` `  `    ``// Initialize cnt array to ` `    ``// store digit count ` `    ``int` `[]cnt = ``new` `int``; ` ` `  `    ``// Iterate through the digits of N ` `    ``while` `(N > 0) ` `    ``{ ` ` `  `        ``// Retrieve the last digit of N ` `        ``int` `rem = N % 10; ` ` `  `        ``// Increase the count of digit ` `        ``cnt[rem]++; ` ` `  `        ``// Remove the last digit of N ` `        ``N = N / 10; ` `    ``} ` ` `  `    ``// Iterate through the cnt array ` `    ``for` `(``int` `i = 0; i < 10; i++)  ` `    ``{ ` ` `  `        ``// If frequency of digit ` `        ``// is greater than 1 ` `        ``if` `(cnt[i] > 1)  ` `        ``{ ` ` `  `            ``// Increment the count ` `            ``// of Repeating digits ` `            ``res++; ` `        ``} ` `    ``} ` ` `  `    ``// Return count of repeating digit ` `    ``return` `res; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``// Given array []arr ` `    ``int` `N = 12; ` ` `  `    ``// Function Call ` `    ``Console.WriteLine(countRepeatingDigits(N)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji  `

Output:

```0
```

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.