Count Repeating Digits

Easy
0/40
0 upvote

Problem statement

You are given an integer 'N'.


Return the count of repeating digits in 'N'.


For Example :
Let 'N' = 9397776.
The repeating digits in 'N' are 9 and 7.
Therefore, the answer is 2.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains an integer 'N'.
Output Format :
Return the count of repeating digits in 'N'.
Note :
You don’t need to print anything. Just implement the given function.
Constraints :
0 <= 'N' <= 10^9

Time Limit: 1 sec
Sample Input 1 :
12345
Sample Output 1 :
0
Explanation of sample input 1 :
The digits in the given number are 1, 2, 3, 4, and 5.
None of these digits repeat.
Thus, the answer is 0.
Sample Input 2 :
14312
Sample Output 2 :
1
Hint

Keep track of the digits you have encountered.

Approaches (1)
String Conversion

Approach:

  • Create an array of size 10 to store the frequency of each digit (0-9).
  • Iterate through the digits of the number.
  • For each digit, increment its count in the frequency array.
  • Iterate through the frequency array and count the number of digits with a frequency greater than 1.

Algorithm:

  • Initialize an array 'freq' of size 10 with all elements as 0.
  • Convert the integer 'N' to a string 'S'.
  • Iterate using 'i' from 0 to length of 'S' - 1 :
    • Let 'digit' be the integer value of 'S[i]'.
    • Increment 'freq[digit]' by 1.
  • Initialize a variable 'count' to 0.
  • Iterate using 'i' from 0 to 9 :
    • If ( freq[i] > 1 ) :
      • Increment 'count' by 1.
  • Return 'count'.
Time Complexity

O(D), where 'D' is the number of digits in 'N'.

We iterate through the digits of 'N' once and then iterate through a constant size array (10). Thus the overall time complexity is of the order O(D).

Space Complexity

O(1).

We create an array of size 10 to store the frequency of digits. Thus, the overall space complexity is of the order O(1).

Code Solution
(100% EXP penalty)
Count Repeating Digits
Full screen
Console