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
# Python3 code to demonstrate working of
# Remove numbers with repeating digits
# Using set() + len() + list comprehension
# initializing list
test_list = [4252, 6578, 3421, 6545, 6676]
# printing original list
print("The original list is : " + str(test_list))
# set() used to remove digits
res = [sub for sub in test_list if len(set(str(sub))) == len(str(sub))]
# printing result
print("List after removing repeating digit numbers : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Remove numbers with repeating digits
# Using regex()
import re
# initializing list
test_list = [4252, 6578, 3421, 6545, 6676]
# printing original list
print("The original list is : " + str(test_list))
# regex used to remove elements with repeating digits
regex = re.compile(r"(\d).*\1")
res = [sub for sub in test_list if not regex.search(str(sub))]
# printing result
print("List after removing repeating digit numbers : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Remove numbers with repeating digits
from collections import Counter
# initializing list
test_list = [4252, 6578, 3421, 6545, 6676]
# printing original list
print("The original list is : " + str(test_list))
res = [sub for sub in test_list if len(Counter(str(sub))) == len(str(sub))]
# printing result
print("List after removing repeating digit numbers : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Remove numbers with repeating digits
# Using set() + all() + list comprehension
# initializing list
test_list = [4252, 6578, 3421, 6545, 6676]
# printing original list
print("The original list is : " + str(test_list))
# set() and all() used to remove digits
res = [sub for sub in test_list if all(str(sub).count(i) == 1 for i in str(sub))]
# printing result
print("List after removing repeating digit numbers : " + str(res))
OutputThe 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
# Function to check if all digits in a number are unique
def is_unique(num):
digits = str(num)
return len(digits) == len(set(digits))
# initializing list
test_list = [4252, 6578, 3421, 6545, 6676]
# printing original list
print("The original list is : " + str(test_list))
# filter numbers with unique digits
result = []
for num in test_list:
if is_unique(num):
result.append(num)
# printing result
print("List after removing repeating digit numbers : " + str(result))
OutputThe 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
# initializing list
test_list = [4252, 6578, 3421, 6545, 6676]
# printing original list
print("The original list is : " + str(test_list))
# convert list to NumPy array
arr = np.array(test_list)
# vectorized function to check for repeating digits
def has_repeating_digits(n):
return all(np.char.count(str(n), c) == 1 for c in str(n))
# vectorized function to remove repeating digit numbers
remove_mask = np.vectorize(has_repeating_digits)(arr)
res = arr[remove_mask]
# convert back to Python list
res = res.tolist()
# printing result
print("List after removing repeating digit numbers : " + str(res))
#This code is contributed by Pushpa
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)
Similar Reads
Python | Ways to remove numeric digits from given string In Python, we can remove numeric digits from a string in multiple ways. For example, if we have a string like "Geeks123" and we want to remove the numbers to get "Geeks", we can use different methods to accomplish this. We can do this by using techniques such as list comprehension, regular expressio
3 min read
Python - Retain Numbers in String Retaining numbers in a string involves extracting only the numeric characters while ignoring non-numeric ones.Using List Comprehensionlist comprehension can efficiently iterate through each character in the string, check if it is a digit using the isdigit() method and join the digits together to for
2 min read
Python - Extract Strings with a digit Given a Strings List, extract those with atleast one digit. Input : test_list = ['gf4g', 'is', 'best', 'gee1ks'] Output : ['gf4g', 'gee1ks'] Explanation : 4, 1 are respective digits in string. Input : test_list = ['gf4g', 'is', 'best', 'geeks'] Output : ['gf4g'] Explanation : 4 is digit in string. M
5 min read
Python | Remove all digits from a list of strings The problem is about removing all numeric digits from each string in a given list of strings. We are provided with a list where each element is a string and the task is to remove any digits (0-9) from each string, leaving only the non-digit characters. In this article, we'll explore multiple methods
4 min read
Python - Remove all digits before given Number Given a String, remove all numeric digits before K number. Method #1 : Using split() + enumerate() + index() + list comprehension This is one of the ways in which this task can be performed. In this, we perform task of split() to get all words, getting index of K number using index() and list compre
6 min read