Python – Remove numbers with repeating digits
Last Updated :
11 May, 2023
Given a list of numbers, the task is to write a Python program to remove all numbers with repetitive digits.
Examples:
Input : test_list = [4252, 6578, 3421, 6545, 6676]
Output : test_list = [6578, 3421]
Explanation : 4252 has 2 occurrences of 2 hence removed. Similar case for all other removed.
Input : test_list = [4252, 6578, 3423, 6545, 6676]
Output : test_list = [6578]
Explanation : 4252 has 2 occurrences of 2 hence removed. Similar case for all other removed.
Method 1 : Using set() + len() + list comprehension
In this, we perform the task of eliminating repeating elements using set() and then compare the length to be equal to the original length. If found, True is returned, else False is returned.
Python3
test_list = [ 4252 , 6578 , 3421 , 6545 , 6676 ]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if len ( set ( str (sub))) = = len ( str (sub))]
print ( "List after removing repeating digit numbers : " + str (res))
|
Output
The original list is : [4252, 6578, 3421, 6545, 6676]
List after removing repeating digit numbers : [6578, 3421]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using regex()
Appropriate regex can also be used for the task of checking for each digit repetition only once.
Python3
import re
test_list = [ 4252 , 6578 , 3421 , 6545 , 6676 ]
print ( "The original list is : " + str (test_list))
regex = re. compile (r "(\d).*\1" )
res = [sub for sub in test_list if not regex.search( str (sub))]
print ( "List after removing repeating digit numbers : " + str (res))
|
Output
The original list is : [4252, 6578, 3421, 6545, 6676]
List after removing repeating digit numbers : [6578, 3421]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using the Counter() function
Python3
from collections import Counter
test_list = [ 4252 , 6578 , 3421 , 6545 , 6676 ]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if len (Counter( str (sub))) = = len ( str (sub))]
print ( "List after removing repeating digit numbers : " + str (res))
|
Output
The original list is : [4252, 6578, 3421, 6545, 6676]
List after removing repeating digit numbers : [6578, 3421]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 4: Using set() + all() + list comprehension
Python3
test_list = [ 4252 , 6578 , 3421 , 6545 , 6676 ]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if all ( str (sub).count(i) = = 1 for i in str (sub))]
print ( "List after removing repeating digit numbers : " + str (res))
|
Output
The original list is : [4252, 6578, 3421, 6545, 6676]
List after removing repeating digit numbers : [6578, 3421]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 5: Using a for loop and is_unique function
You can define a separate function is_unique(num) that checks if all the digits in the number are unique. Then, loop through the list and add only the numbers with unique digits to a new list.
step-by-step approach:
- Define a function is_unique(num) that takes an integer as input and returns True if all its digits are unique, False otherwise.
- Initialize an empty list result to store the filtered numbers.
- Loop through the list test_list.
- For each number num in the list, call the is_unique() function to check if it has unique digits.
- If is_unique(num) returns True, append num to the result list.
- Return the result list.
Python3
def is_unique(num):
digits = str (num)
return len (digits) = = len ( set (digits))
test_list = [ 4252 , 6578 , 3421 , 6545 , 6676 ]
print ( "The original list is : " + str (test_list))
result = []
for num in test_list:
if is_unique(num):
result.append(num)
print ( "List after removing repeating digit numbers : " + str (result))
|
Output
The original list is : [4252, 6578, 3421, 6545, 6676]
List after removing repeating digit numbers : [6578, 3421]
Time complexity: O(n*k), where n is the length of test_list and k is the average number of digits in the integers.
Auxiliary space: O(k) for the is_unique() function, and O(n) for the result list.
Method 6: Using numpy:
Algorithm:
- Initialize the input list of numbers.
- Convert the list to a NumPy array.
- Define a vectorized function to check if a number has repeating digits.
- Use the vectorized function to generate a boolean mask indicating which elements of the array have repeating digits.
- Use the boolean mask to remove the elements with repeating digits from the array.
- Convert the filtered array back to a Python list.
- Print the final list without the numbers with repeating digits.
Python3
import numpy as np
test_list = [ 4252 , 6578 , 3421 , 6545 , 6676 ]
print ( "The original list is : " + str (test_list))
arr = np.array(test_list)
def has_repeating_digits(n):
return all (np.char.count( str (n), c) = = 1 for c in str (n))
remove_mask = np.vectorize(has_repeating_digits)(arr)
res = arr[remove_mask]
res = res.tolist()
print ( "List after removing repeating digit numbers : " + str (res))
|
Output:
The original list is : [4252, 6578, 3421, 6545, 6676]
List after removing repeating digit numbers : [6578, 3421]
Time complexity: O(N*log(N)) (converting list to NumPy array)
Space complexity: O(N) (storing the NumPy array and the filtered array)
Share your thoughts in the comments
Please Login to comment...