Open In App

Python – Remove numbers with repeating digits

Last Updated : 11 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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))


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




# 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))


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




# 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))


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




# 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))


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:

  1. Define a function is_unique(num) that takes an integer as input and returns True if all its digits are unique, False otherwise.
  2. Initialize an empty list result to store the filtered numbers.
  3. Loop through the list test_list.
  4. For each number num in the list, call the is_unique() function to check if it has unique digits.
  5. If is_unique(num) returns True, append num to the result list.
  6. 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))


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:

  1. Initialize the input list of numbers.
  2. Convert the list to a NumPy array.
  3. Define a vectorized function to check if a number has repeating digits.
  4. Use the vectorized function to generate a boolean mask indicating which elements of the array have repeating digits.
  5. Use the boolean mask to remove the elements with repeating digits from the array.
  6. Convert the filtered array back to a Python list.
  7. 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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads