Find Missing Element in integer List Problem statement: Given an integer list from 0 to 100 find the missing Element.

I saw this problem on reddit on a compilation of interview questions, tried it out for fun. Mainly tried to generalize the code for any boundry(not just 100) and multiple missing arguments. Focused on readability and writing docstrings based on Google's recommendations. Did I hit my targets? How can and should my code be improved. Thanks!

"""
    Problem: Given a list from 0 to 100, find the missing element
"""

def makeIntArray(excludedValues, boundry):
    return [i for i in range(boundry + 1) if i not in excludedValues]

def compareToComplete(partial):
    """ Compares a possibly partial list to its complete version

        Args:
            partial: List containing integer type, all duplicates will be ignored

        Returns: 
            Set of unique  missing elements or empty set if partial is complete  

        example:
             partial = [1,2,3,4,6,8,10], complete = [0,1,2,3,4,5,6,7,8,9,10]
             returns: {0,5,7,9}
    """
    partial.sort()
    return {i for i in range(partial[-1] + 1)}.difference(set(partial))
don't change the input unless explicitly stated

partial.sort() works in-place. This might cause some nasty surprises for someone who uses this function and did not expect this.

The reason you sort this is also only to take the last element. max(partial) would suffice here. If partial is sorted, you can work with a different algorithm using the sortedness

set comprehension

a set comprehension is nice, but here a set(range(partial[-1] + 1)) would suffice

set.difference

there is no need to cast partial to a set

so the complete method reduces to 

return set(range(max(partial) + 1)).difference(partial)


alternative method

If partial is sorted, you can work with 2 iterators

from itertools import count

def compare_to_complete_generator(partial):
    complete_iterator = count()
    partial_iterator = iter(partial)

    element_complete= next(complete_iterator)
    element_partial = next(partial_iterator)

    while True:
        while element_partial != element_complete:
            yield element_complete
            element_complete = next(complete_iterator)
        element_complete = next(complete_iterator)
        try:
            element_partial = next(partial_iterator)
        except StopIteration:
            return

list(compare_to_complete(partial))



[0, 5, 7, 9]



For large lists, this has the advantage that you don't have to look for the max and construct a set with max elements. This also accepts any sorted iterable or iterator, not just lists. if the input is not sorted, you can use sorted, but then you lose the advantages of this method.