---
# System prepended metadata

title: Find One Missing Element From List ( diff methods )
tags: [Experiments, Python]

---

###### tags: `Python` `Experiments`

# Find One Missing Element From List ( diff methods )

``` python
from memory_profiler import memory_usage
from timeit import timeit


def findmissing_set(list_1, list_2):
    missing = set(list_1) - set(list_2)
    assert(len(missing) == 1), "There are more than one number missing"
    return list(missing)[0]


def findmissing_in(list_1, list_2):
    for i in list_1:
        if not i in list_2:
            return i


def findmissing_idx(list_1, list_2):
    for i in list_1:
        try:
            list_2.index(i)
        except ValueError:
            return i


def findmissing_xor(list_1, list_2):
    sum_xor = int()
    for i in list_1:
        sum_xor ^= i

    for i in list_2:
        sum_xor ^= i
    
    return sum_xor

def findmissing_sum(list_1, list_2):
    sum1 = int()
    sum2 = int()
    for i in list_1:
        sum1 += i
    for i in list_2:
        sum2 += i
    return sum1-sum2

if __name__ == '__main__':

    # print(findmissing_set([4, 12, 9, 5, 16], [4, 12, 9, 16]))
    # print(findmissing_in([4, 12, 9, 5, 16], [4, 12, 9, 16]))
    # print(findmissing_idx([4, 12, 9, 5, 16], [4, 12, 9, 16]))
    # print(findmissing_xor([4, 12, 9, 5, 16], [4, 12, 9, 16]))
    # print(findmissing_sum([4, 12, 9, 5, 16], [4, 12, 9, 16]))

    list_1 = [integer for integer in range(0, 100)]
    list_2 = [integer for integer in range(0, 99)]

    print("findmissing_set :\n\ttime: ", timeit(
        "findmissing_set({}, {})".format(list_1,list_2), setup="from __main__ import findmissing_set"))
    print("\tmemory : ", memory_usage(
        (findmissing_set, (list_1, list_2))))

    #==============================================================================================
    print("findmissing_in :\n\ttime: ", timeit(
        "findmissing_in({}, {})".format(list_1, list_2), setup="from __main__ import findmissing_in" ))
    print("\tmemory : ", memory_usage(
        (findmissing_in, (list_1, list_2))))

    #==============================================================================================
    print("findmissing_idx :\n\ttime: ", timeit(
        "findmissing_idx({}, {})".format(list_1, list_2), setup="from __main__ import findmissing_idx"))
    print("\tmemory : ", memory_usage(
        (findmissing_idx, (list_1, list_2))))

    #==============================================================================================
    print("findmissing_xor :\n\ttime: ", timeit(
        "findmissing_xor({}, {})".format(list_1, list_2), setup="from __main__ import findmissing_xor"))
    print("\tmemory : ", memory_usage(
        (findmissing_xor, (list_1, list_2))))

    #==============================================================================================
    print("findmissing_sum :\n\ttime: ", timeit(
        "findmissing_sum({}, {})".format(list_1, list_2), setup="from __main__ import findmissing_sum"))
    print("\tmemory : ", memory_usage(
        (findmissing_sum, (list_1, list_2))))

```

## Output
```
findmissing_set:
        time:  15.528574591000002
        memory:  [13.0390625, 13.046875, 13.046875]
findmissing_in:
        time:  138.464003773
        memory:  [13.16015625, 13.16015625,
                  13.16015625, 13.16015625, 13.16015625]
findmissing_idx:
        time:  163.50152925199998
        memory:  [13.2109375, 13.2109375, 13.2109375,  
                  13.2109375, 13.2109375]
findmissing_xor:
        time:  25.572368368000014
        memory:  [13.23046875, 13.23046875, 13.23046875]
findmissing_sum:
        time:  20.179685679999977
        memory:  [13.31640625, 13.31640625, 13.31640625]
```
