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 Remove numbers with repeating digits
In this article, we will learn how to remove numbers with repeating digits from a list in Python. Numbers like 3352 (3 appears twice) or 661 (6 appears twice) will be filtered out, keeping only numbers with unique digits.
Problem Example
Given an input list of numbers, we need to remove all numbers containing repeating digits ?
Input
inputList = [3352, 8135, 661, 7893, 99]
Expected Output
[8135, 7893]
In the above example, 3352 has digit 3 repeated twice, 661 has digit 6 repeated, and 99 has digit 9 repeated. Only 8135 and 7893 have all unique digits.
Method 1: Using List Comprehension and set()
The set() function removes duplicate elements. We can compare the length of the original number with the length of its digit set ?
# input list
inputList = [3352, 8135, 661, 7893, 99]
# printing the input list
print("Input list:", inputList)
# Remove numbers with repeating digits using set()
resultList = [num for num in inputList if len(set(str(num))) == len(str(num))]
# printing resultant list
print("Resultant list after removing numbers with repeating digits:")
print(resultList)
Input list: [3352, 8135, 661, 7893, 99] Resultant list after removing numbers with repeating digits: [8135, 7893]
How It Works
For each number, we convert it to string, create a set of its digits, and compare lengths. If len(set(str(num))) == len(str(num)), all digits are unique.
Method 2: Using Regular Expression
We can use regex pattern (\d).*\1 to find numbers where any digit appears more than once ?
import re
# input list
inputList = [3352, 8135, 661, 7893, 99]
print("Input list:", inputList)
# regex pattern to find repeating digits
regexPattern = re.compile(r"(\d).*\1")
# Keep numbers that DON'T match the pattern
resultList = [num for num in inputList if not regexPattern.search(str(num))]
print("Resultant list after removing numbers with repeating digits:")
print(resultList)
Input list: [3352, 8135, 661, 7893, 99] Resultant list after removing numbers with repeating digits: [8135, 7893]
Regex Pattern Explanation
The pattern (\d).*\1 captures a digit (\d), then matches any characters .*, then matches the same captured digit again \1.
Method 3: Using Counter()
The Counter() function from collections module counts occurrences of each digit. If any digit has count > 1, the number has repeating digits ?
from collections import Counter
# input list
inputList = [3352, 8135, 661, 7893, 99]
print("Input list:", inputList)
# Use Counter to check for unique digits
resultList = [num for num in inputList if len(Counter(str(num))) == len(str(num))]
print("Resultant list after removing numbers with repeating digits:")
print(resultList)
Input list: [3352, 8135, 661, 7893, 99] Resultant list after removing numbers with repeating digits: [8135, 7893]
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
set() |
O(n) | O(1) | Simple and readable |
| Regular Expression | O(n) | O(1) | Pattern matching scenarios |
Counter() |
O(n) | O(1) | When you need digit frequencies |
Conclusion
Use set() for the simplest and most readable solution. The regex approach is useful for complex pattern matching, while Counter() provides detailed digit frequency information. All methods have O(n) time complexity and work efficiently for this problem.
