For a function to determine which element has been removed from a shuffled array, I came up with:
def finder(arr1, arr2): l = len(arr1) - 1 result = list(arr1) for i in range(l): current = arr1[i] if current in arr2: result.remove(current) arr2.remove(current) return result
In the solution for my course it says:
The naive solution is go through every element in the second array and check whether it appears in the first array. Note that there may be duplicate elements in the arrays so we should pay special attention to it. The complexity of this approach is \$O(N^2)\$, since we would need two for loops.
However doing the other way round, i.e. as I've done above seems to avoid a nested for loop, and is therefore linear as far as I can tell. Am I missing something? Have I made an error in my code or my analysis of its efficiency?