Python - Big-O of del my_list[:]?

python

Solution

The `del` doesn't impact big-O here, the loop is order `n` and the `j in A` test is order `n`, so the nested loop is `O(n**2)`; the `del` is `O(n)`, but it's not part of the loop, and since it's a lower order of work, it's ignored.

Side-note: A `O(n)` solution for this would be to use `collections.OrderedDict` to dedup, preserving order, making the body of the method just:

A[:] = collections.OrderedDict.fromkeys(A)
return len(A)

Problem

What is the big O of del my_list[:]? This command deletes all elements in the list. My understanding is that it will be O(n). n being the length of the list. Therefore the big O of this code would be bigO(n^2), correct? Note this is not for school, but rather for my understanding while I practice for interviews. ``` from copy import deepcopy class Solution: # @param A : list of integers # @return an integer def removeDuplicates(self, A): copy_array = deepcopy(A) del A[:] for j in copy_array: if j in A: pass else: A.append(j) return len(A) ```

Original source