Python – Find Numbers in Range and not in Set
Given a set and range of numbers, the task is to write a Python program to extract all numbers in the range not in set.
Examples:
Input : test_set = {6, 4, 2, 7, 9}, low, high = 5, 10
Output : [5, 8]
Explanation : 6, 7 and 9 are present in set, remaining 5, 8 are in output.
Input : test_set = {6, 4, 2, 7, 9}, low, high = 5, 8
Output : [5]
Explanation : 6 and 7 are present in set, remaining 5 is in output.
Method #1: Using loop
In this, we iterate for all the elements in range and using conditional statements omit the elements from result which are not present in set.
Python3
test_set = { 6 , 4 , 2 , 7 , 9 }
print ( "The original set is : " + str (test_set))
low, high = 5 , 10
res = []
for ele in range (low, high):
if ele not in test_set:
res.append(ele)
print ( "Elements not in set : " + str (res))
|
Output
The original set is : {2, 4, 6, 7, 9}
Elements not in set : [5, 8]
Method #2: Using “-” operator
In this, we perform the task of performing getting differences from range through set elements using the “-” operator.
Python3
test_set = { 6 , 4 , 2 , 7 , 9 }
print ( "The original set is : " + str (test_set))
low, high = 5 , 10
res = list ( set ( range (low, high)) - test_set)
print ( "Elements not in set : " + str (res))
|
Output
The original set is : {2, 4, 6, 7, 9}
Elements not in set : [8, 5]
Method #3 : Using Counter() function
Python3
from collections import Counter
test_set = { 6 , 4 , 2 , 7 , 9 }
freq = Counter(test_set)
print ( "The original set is : " + str (test_set))
low, high = 5 , 10
res = []
for ele in range (low, high):
if ele not in freq.keys():
res.append(ele)
print ( "Elements not in set : " + str (res))
|
Output
The original set is : {2, 4, 6, 7, 9}
Elements not in set : [5, 8]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using list comprehension
Here, the range() function is used to generate a range of numbers from low to high (inclusive), and a list comprehension is used to iterate over the range of numbers and only include those that are not present in the given set test_set. The resulting list of missing numbers is then returned.
Python3
def find_missing_numbers(test_set, low, high):
return [i for i in range (low, high) if i not in test_set]
low, high = 5 , 10
test_set = { 2 , 4 , 6 , 7 , 9 }
print (find_missing_numbers(test_set, low, high))
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #5: Using filter() and lambda function with set membership:
Algorithm:
- Initialize a set of numbers (test_set) and a range of numbers (low and high).
- Use the range() function to generate a sequence of numbers from low to high (excluding high).
- Use the filter() function to apply a lambda function to each number in the range, and return only those that are not in the test_set.
- Convert the filtered sequence to a list using the list() function.
- Print the resulting list of numbers not in the test_set.
Example:
Python3
test_set = { 6 , 4 , 2 , 7 , 9 }
print ( "The original set is : " + str (test_set))
low, high = 5 , 10
res = list ( filter ( lambda x: x not in test_set, range (low, high)))
print ( "Elements not in set : " + str (res))
|
Output
The original set is : {2, 4, 6, 7, 9}
Elements not in set : [5, 8]
Time complexity: O(n), where n is the size of the range from low to high. This is because the filter() function applies the lambda function to each element in the range, so the time complexity is proportional to the size of the range.
Auxiliary Space: O(n), where n is the size of the range from low to high. This is because the resulting list of filtered numbers has to be stored in memory.
Method #6: Using Set Difference
Python3
test_set = { 6 , 4 , 2 , 7 , 9 }
print ( "The original set is : " + str (test_set))
low, high = 5 , 10
res = set ( range (low, high)) - test_set
res = sorted ( list (res))
print ( "Elements not in set : " + str (res))
|
Output
The original set is : {2, 4, 6, 7, 9}
Elements not in set : [5, 8]
Time complexity O(high-low) because creating a set of the range and taking set differences takes linear time complexity in the length of the range.
Auxiliary space: O(high-low) for creating the set of the range, O(n) for the test_set, and O(high-low) for the result list.
Last Updated :
25 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...