Given a number N, the task is to count the total number of repeating digits in the given number.
Input: N = 99677
In the given number only 9 and 7 are repeating, hence the answer is 2.
Input: N = 12
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:
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.
- Find smallest number with given number of digits and sum of digits under given constraints
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Smallest number with given sum of digits and sum of square of digits
- Find the average of k digits from the beginning and l digits from the end of the given number
- Sum of the digits of square of the given number which has only 1's as its digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Find smallest possible Number from a given large Number with same count of digits
- Count of pairs with difference at most K with no element repeating
- Count all distinct pairs of repeating elements from the array for every array element
- Minimum number of digits to be removed so that no two consecutive digits are same
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.