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)
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 fo
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 - Sort list of numbers by sum of their digits
Sorting a list of numbers by the sum of their digits involves ordering the numbers based on the sum of each individual digit within the number. This approach helps prioritize numbers with smaller or larger digit sums, depending on the use case. Using sorted() with a Lambda Functionsorted() function
2 min read
Python Program to Reverse a Number
We are given a number and our task is to reverse its digits. For example, if the input is 12345 then the output should be 54321. In this article, we will explore various techniques for reversing a number in Python. Using String SlicingIn this example, the Python code reverses a given number by conve
3 min read
Python - Extract Rear K digits from Numbers
Given an Integer list, extract rear K digits from it. Input : test_list = [5645, 23567, 34543, 87652, 2342], K = 2 Output : [45, 67, 43, 52, 42] Explanation : Last 2 digits extracted. Input : test_list = [5645, 23567, 34543, 87652, 2342], K = 4 Output : [5645, 3567, 4543, 7652, 2342] Explanation : L
5 min read
Python | Removing duplicates from tuple
Many times, while working with Python tuples, we can have a problem removing duplicates. This is a very common problem and can occur in any form of programming setup, be it regular programming or web development. Let's discuss certain ways in which this task can be performed. Method #1 : Using set()
4 min read
Python - Remove digits from Dictionary String Values List
We are given dictionary we need to remove digits from the dictionary string value list. For example, we are given a dictionary that contains strings values d = {'key1': ['abc1', 'def2', 'ghi3'], 'key2': ['xyz4', 'lmn5']} we need to remove digits from dictionary so that resultant output becomes {'key
3 min read
Python - Splitting Text and Number in string
Given a string containing both letters and numbers, the task is to separate the text (letters) and the numbers into two separate outputs. For example, if the input is "abc123", the output should be "abc" and "123". Let's discuss various ways in which we can achieve this in Python. Using for loopThis
3 min read