In this tutorial, we will learn about how to find unique digits in an integer in Python with a coding example. In many situations, you might have to come up with this type of requirements.
I know you are here just because you are in need of this awesome trick to find unique digits in an integer that means user get the unique digits which digits are not repeated in that integer and also count the total number of unique digits present in that integer, if there are no unique digits then print that – “There are no unique digit” in Python.
If you don’t know how to ignore all the duplicate values and find the unique values from an integer using string list and count then you are at the right place. Because in this tutorial we will understand how we can find the unique value from an integer using python. And this method choose to save time and hustle.
Find unique digits in an integer in Python
Let’s learn this with a easy coding example,
1. At first, create an integer variable which can hold the value entered by the user. (user input)
s = int(input("Enter your value :"))
2. Then Convert to list of digits –
a = [int(digit) for digit in str (s)]
– Converts the integer s into a string (str(s)), then iterates over each character in the string (digit), converting it back to an integer (int(digit)).
– This creates a list a where each element is a digit from the integer s.
3. Then Initialize an empty list –
l=[]
Creates an empty list l where unique digits will be stored.
4. After that, Iterate through the list –
for i in range (0,n):
– n = len(a) gets the length of the list a.
– The outer loop (for i) iterates through each digit in the list a.
5. Then Count occurrences –
c=0
Initializes a counter c to 0 for counting occurrences of each digit.
6. Next Nested loop for comparison –
for j in range(0,n):
The inner loop (for j) compares the digit at index i with every other digit in the list (a[j]).
7. Then Check for uniqueness –
if a[i]==a[j] and i!=j: c=c+1
– if a[i]==a[j] and i!=j: checks if the digit at index i is equal to the digit at index j and they are not the same index (i != j).
– If they are equal (a[i]==a[j] and i!=j), increment the counter c.
8. Then Add unique digits to list –
if c==0: l.append(a[i])
– After the inner loop, if c remains 0, it means the digit at index i is unique within the list.
– Append this unique digit (a[i]) to the list l.
9. Next Update list a –
a=l
Assigns the list a to the list l, which now contains only unique digits.
10. Finally, Output section –
if len(l)==0: print("There are no unique digit") else: print("Total no of unique digits are :", len(l)) print("Unique digits are :",l)
– if len(l)==0: checks if the list l is empty, indicating no unique digits were found.
– If l is empty, print “There are no unique digit”.
– Otherwise, print the total number of unique digits and count them.
Python Code – “Find unique digits in an integer”
# Step 1: Take user input and convert it to an integer s = int(input("Enter your value :")) # Step 2: Convert the integer to a list of its digits a = [int(digit) for digit in str(s)] # Step 3: Initialize an empty list to store unique digits l = [] # Step 4: Get the length of the list 'a' n = len(a) # Step 5: Outer loop to iterate through each digit in 'a' for i in range(0, n): # Step 6: Initialize a counter 'c' to count occurrences c = 0 # Step 7: Inner loop to compare current digit with other digits in 'a' for j in range(0, n): # Step 8: Check if digits are equal and not at the same index if a[i] == a[j] and i != j: # Step 9: Increment counter if duplicate is found c = c + 1 # Step 10: If no duplicates found (c == 0), append digit to list 'l' if c == 0: l.append(a[i]) # Step 11: Update 'a' to contain only unique digits a = l # Step 12: Check if 'l' (which now holds unique digits) is empty if len(l) == 0: # Step 13: Print message if no unique digits found print("There are no unique digit") else: # Step 14: Print total number of unique digits print("Total no of unique digits are :", len(l)) # Step 15: Print the list of unique digits print("Unique digits are :", l)
Let’s take an Input,
If the user enter – 1513985
Then the Output :
Enter your value :1513985 Total no of unique digits are : 3 Unique digits are : [3, 9, 8]
Explanation:
– Input string “1513985” is converted to list [‘1’, ‘5’, ‘1’, ‘3’, ‘9’, ‘8’, ‘5’].
– The code identifies unique digits ‘3’, ‘9’ and ‘8’ and stores them in list l.
– len(l) is 3, so it prints the count of unique digits and lists them.
Let’s take another Input,
If the user enter – 5555
Then the Output :
Enter your value :5555 There are no unique digit
Explanation:
In 5555, one digit (5) are repeated many times and here has no unique digit. That’s why print “There are no unique digit”.