Given a number N, the task is to print all duplicate digits of that number in sorted order.
Examples:
Input: 123412989
Output : 3 4 8
Explanation : The unique digits are 3 4 8
Input : 9832213789
Output: 1 7
Approach:
The idea is to use The idea is to use Hashing to store the frequency of the digits. Traverse the Hashtable and then check digits with the frequency greater than 1 and then print these digits. Follow the steps below to solve the problem. Create a HashTable of size 10 for digits 0-9. Initially store each index as 0. Convert integer to string. Now for each character in string convert character to integer using typecasting Increment the count of that index in the hashtable. Traverse the hashtable and print the indices that have value greater than 1. Since we traverse in hashtable the digits are printed in sorted order. Hashing to store the frequency of the digits.
Traverse the Hashtable and then check digits with the frequency equal to 1 and then print these digits.
Follow the steps below to solve the problem.
- Create a HashTable of size 10 for digits 0-9. Initially store each index as 0.
- Convert integer to string.
- Now for each character in string convert character to integer using typecasting
- Increment the count of that index in the hashtable.
- Traverse the hashtable and print the indices that have value equal to 1.
- Since we traverse in hashtable the digits are printed in sorted order.
Below is the implementation of above approach
# Python implementation
# of above approach
# Function which print Unique digits
def printUnique(N):
# Initialize cnt list to store
# digit count
cnt = [0] * 10
# converting integer to string
string = str(N)
# Traversing the string
for i in string:
# converting character to integer
digit = int(i)
# Increase the count of digit
cnt[digit] += 1
# Iterate through the cnt list
for i in range(10):
# If frequency of
# digit is 1
if (cnt[i] == 1):
# If the value is equal to 1 then print
print(i, end=" ")
# Driver Code
# Input number
N = 12345674321
# Function call to print Unique digits of the number N
printUnique(N)
Output:
5 6 7
Time Complexity: O(n) where n is length of number
Method #2: Using Built in Python Functions
Approach:
- Convert the integer to string.
- Calculate the frequencies of every character using Counter() function.
- Traverse in the sorted frequency dictionary
- Print the keys which are having value 1
Below is the implementation
# Python implementation
# of above approach
from collections import Counter
# Function which print Unique digits
def printUnique(N):
# Initialize a variable to store
# count of unique digits
# converting integer to string
string = str(N)
# Calculating frequencies using Counter
frequency = Counter(string)
# Traveerse in the sorted frequency dictionary
for i in sorted(frequency):
# check if any value is 1
if(frequency[i] == 1):
print(i, end=" ")
# Driver Code
# Input number
N = 12345674321
# Function call to print Unique digits of the number N
printUnique(N)
Output:
5 6 7
Time Complexity: O(n log n) where n is length of number.
