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
# Python3 code to demonstrate working of # Range Numbers not in set # Using loop # initializing set test_set = { 6 , 4 , 2 , 7 , 9 } # printing original set print ( "The original set is : " + str (test_set)) # initializing range low, high = 5 , 10 res = [] for ele in range (low, high): # getting elements not in set if ele not in test_set: res.append(ele) # printing result print ( "Elements not in set : " + str (res)) |
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
# Python3 code to demonstrate working of # Range Numbers not in set # Using "-" operator # initializing set test_set = { 6 , 4 , 2 , 7 , 9 } # printing original set print ( "The original set is : " + str (test_set)) # initializing range low, high = 5 , 10 # using "-" operator to get difference res = list ( set ( range (low, high)) - test_set) # printing result print ( "Elements not in set : " + str (res)) |
The original set is : {2, 4, 6, 7, 9} Elements not in set : [8, 5]
Method #3 : Using Counter() function
Python3
# Python3 code to demonstrate working of # Range Numbers not in set # Using loop from collections import Counter # initializing set test_set = { 6 , 4 , 2 , 7 , 9 } freq = Counter(test_set) # printing original set print ( "The original set is : " + str (test_set)) # initializing range low, high = 5 , 10 res = [] for ele in range (low, high): # getting elements not in set if ele not in freq.keys(): res.append(ele) # printing result print ( "Elements not in set : " + str (res)) |
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
# Define a function that takes in a set of integers and two integers specifying a range def find_missing_numbers(test_set, low, high): # Use a list comprehension to generate a list of integers in the range from low to high, # excluding any integers that are in the test_set return [i for i in range (low, high) if i not in test_set] # Define the range and test set of integers low, high = 5 , 10 test_set = { 2 , 4 , 6 , 7 , 9 } # Call the find_missing_numbers function with the test_set and range, and print the output print (find_missing_numbers(test_set, low, high)) |
[5, 8]
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
# initializing set test_set = { 6 , 4 , 2 , 7 , 9 } # printing original set 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))) # printing result print ( "Elements not in set : " + str (res)) |
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
# Python3 code to demonstrate working of # Range Numbers not in set # Using set difference method # initializing set test_set = { 6 , 4 , 2 , 7 , 9 } # printing original set print ( "The original set is : " + str (test_set)) # initializing range low, high = 5 , 10 # using set difference to get elements not in set res = set ( range (low, high)) - test_set # converting set to list and sorting res = sorted ( list (res)) # printing result print ( "Elements not in set : " + str (res)) |
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.