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
Python Program to print all distinct uncommon digits present in two given numbers
When it is required to print all the distinct uncommon digits that are present in two numbers, a method is defined that takes two integers as parameters. The method symmetric_difference() is used to get the uncommon digits that exist in one number but not in both.
What are Uncommon Digits?
Uncommon digits are digits that appear in one number but not in the other. For example, in numbers 567234 and 87953573214, the uncommon digits are 1, 6, 8, and 9.
Example
Below is a demonstration of finding distinct uncommon digits ?
def distinct_uncommon_nums(val_1, val_2):
# Convert numbers to strings to access individual digits
val_1 = str(val_1)
val_2 = str(val_2)
# Convert string digits to integers and create lists
digits_1 = list(map(int, val_1))
digits_2 = list(map(int, val_2))
# Convert to sets to get unique digits
set_1 = set(digits_1)
set_2 = set(digits_2)
# Find symmetric difference (uncommon digits)
uncommon_digits = set_1.symmetric_difference(set_2)
# Convert to list and sort in descending order
result = list(uncommon_digits)
result.sort(reverse=True)
return result
# Test the function
num_1 = 567234
num_2 = 87953573214
print("The value of first number is:")
print(num_1)
print("The value of second number is:")
print(num_2)
uncommon = distinct_uncommon_nums(num_1, num_2)
print("Distinct uncommon digits are:")
for digit in uncommon:
print(digit)
The value of first number is: 567234 The value of second number is: 87953573214 Distinct uncommon digits are: 9 8 6 1
How It Works
The algorithm follows these steps:
Convert to strings: Numbers are converted to strings to access individual digits
Extract digits: Each digit is mapped to integer and stored in a list
Create sets: Lists are converted to sets to eliminate duplicate digits
Find symmetric difference: The
symmetric_difference()method returns digits present in either set but not in bothSort results: Uncommon digits are sorted in descending order for display
Alternative Approach Using Set Operations
You can also use the XOR operator (^) for symmetric difference ?
def find_uncommon_digits(num1, num2):
# Get unique digits from both numbers
digits1 = set(str(num1))
digits2 = set(str(num2))
# Find symmetric difference using ^ operator
uncommon = digits1 ^ digits2
# Convert to sorted list
return sorted(map(int, uncommon), reverse=True)
# Example usage
number1 = 12345
number2 = 67890
result = find_uncommon_digits(number1, number2)
print(f"Numbers: {number1}, {number2}")
print(f"Uncommon digits: {result}")
Numbers: 12345, 67890 Uncommon digits: [9, 8, 7, 6, 5, 4, 3, 2, 1]
Conclusion
Use symmetric_difference() or the XOR operator (^) to find digits that exist in one number but not both. Converting numbers to sets automatically handles duplicate digits and makes comparison efficient.
