Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Print all repeating digits present in a given number in sorted order
Finding repeating digits in a number is a common programming task. Python provides several built-in functions like count(), Counter(), and operator.countOf() that can efficiently identify and sort duplicate digits.
Problem Statement
Given a number, we need to find all digits that appear more than once and display them in sorted order.
Input
inputNum = 5322789124199
Expected Output
1 2 9
In the input number 5322789124199, digits 1, 2, and 9 appear multiple times, so they are displayed in ascending order.
Method 1: Using count() Function
The count() method returns the number of occurrences of a substring in a string. We convert the number to a string and check each digit's frequency.
Algorithm
- Convert the input number to a string
- Initialize an empty list for storing duplicate digits
- Traverse each character and check if its count is greater than 1
- Add unique duplicates to the result list
- Sort the list and display the result
Example
# input number
inputNum = 5322789124199
# storing resultant repeating digits in a list
duplicatesList = []
# converting input number to a string
newString = str(inputNum)
# traversing through each character in the string
for c in newString:
# checking if frequency > 1 and not already in duplicates list
if newString.count(c) > 1 and c not in duplicatesList:
# appending character to duplicates list
duplicatesList.append(c)
# sorting the resultant duplicates list
duplicatesList.sort()
# converting list to string and printing
print(' '.join(duplicatesList))
1 2 9
Method 2: Using Hashing
This approach uses an array to count the frequency of each digit (0-9), then identifies digits with frequency greater than 1.
def getDuplicateNum(inputNum):
# array to count frequency of digits 0-9
count = [0] * 10
# converting input number to string
newString = str(inputNum)
# counting frequency of each digit
for c in newString:
curr_digit = int(c)
count[curr_digit] += 1
# checking for digits with frequency > 1
result = []
for k in range(10):
if count[k] > 1:
result.append(str(k))
return ' '.join(result)
# input number
inputNum = 5322789124199
print(getDuplicateNum(inputNum))
1 2 9
Method 3: Using Counter() Function
The Counter class from the collections module creates a frequency dictionary of characters automatically.
from collections import Counter
# input number
inputNum = 5322789124199
# converting input number to string
newString = str(inputNum)
# getting frequency of each character
charFrequency = Counter(newString)
# finding digits with frequency > 1
duplicatesList = []
for digit, frequency in charFrequency.items():
if frequency > 1:
duplicatesList.append(digit)
# sorting and displaying result
duplicatesList.sort()
print(' '.join(duplicatesList))
1 2 9
Method 4: Using operator.countOf() Function
The operator.countOf() function returns the number of occurrences of a value in a sequence.
import operator as op
# input number
inputNum = 5322789124199
# storing resultant repeating digits
duplicatesList = []
# converting input number to string
newString = str(inputNum)
# traversing each character
for c in newString:
# checking if frequency > 1 and not already added
if op.countOf(newString, c) > 1 and op.countOf(duplicatesList, c) == 0:
duplicatesList.append(c)
# sorting and displaying result
duplicatesList.sort()
print(' '.join(duplicatesList))
1 2 9
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| count() | O(n²) | O(1) | Simple implementation |
| Hashing | O(n) | O(1) | Efficient for digits only |
| Counter() | O(n) | O(k) | Readable code |
| operator.countOf() | O(n²) | O(1) | Functional programming style |
Conclusion
The Counter() method provides the most readable solution, while the hashing approach offers the best time complexity. Choose the method based on your specific requirements for readability and performance.
