Find missing elements in a list created from a sequence of consecutive integers with duplicates in O(n)

You can implement an algorithm where you loop through each element of the list and set each element at index i to a negative integer if the list contains the element i as one of the values,. You can then add each index i which is positive to your list of missing items. It doesn't take any additional space and uses at the most 3 for loops(not nested), which makes the complexity O(3*n), which is basically O(n). This site explains it much better and also provides the source code.

edit- I have added the code in case someone wants it:

#The input list and the output list
input = [4, 5, 3, 3, 1, 7, 10, 4, 5, 3]
missing_elements = []

#Loop through each element i and set input[i - 1] to -input[i - 1]. abs() is necessary for 
#this or it shows an error
for i in input:    
    if(input[abs(i) - 1] > 0):
        input[abs(i) - 1] = -input[abs(i) - 1]
    
#Loop through the list again and append each positive value to output list
for i in range(0, len(input)):    
    if input[i] > 0:
        missing_elements.append(i + 1)

For me using loops is not the best way to do it because loops increase the complexity of the given problem. You can try doing it with sets.

def findMissingNums(input_arr):
    
    max_num = max(input_arr) # get max number from input list/array

    input_set = set(input_arr) # convert input array into a set
    set_num = set(range(1,max(input_arr)+1)) #create a set of all num from 1 to n (n is the max from the input array)

    missing_nums = list(set_num - input_set) # take difference of both sets and convert to list/array
    return missing_nums
    
input_arr = [4,3,2,7,8,2,3,1] # 1 <= input_arr[i] <= n
print(findMissingNums(input_arr)) # outputs [5 , 6]```

Tags:

Python