1

I have found this question in a interview

I have array

a = [0,0,1,1,1,2,2,3,4]

I have a sorted array and I want to remove the duplicates from the given array without using any other array, i.e. space complexity should be O(1). Output should be length of the array without duplicates.

Output,

a = [0,1,2,3,4]
length = 5
2
  • Please show an attempt at solving this. Thanks. Commented May 17, 2020 at 5:12
  • use it as a set to non duplicate values in python a = {0,0,1,1,1,2,2,3,4} Commented May 17, 2020 at 5:26

3 Answers 3

4

If you really want this in constant space, you need to be careful not to do anything that will reallocate a new array or a temp array. You can do this by simply overwriting the elements at the front of the array and keeping track of the count. At the end, the solution will be the front slice of the array from 0 to count:

a = [0,0,1,1,1,2,2,3,4]

current = None
count = 0

for n in a:
    if n != current:
        a[count] = n
        count+=1
        current = n

a[:count]  
# [0, 1, 2, 3, 4]

This will be linear time and constant space.

Sign up to request clarification or add additional context in comments.

1 Comment

maybe add a del a[count:] to actually shorten the list.
0

This approach isn't at all time-efficient, but it meets your criteria for space:

>>> a = [0,0,1,1,1,2,2,3,4]
>>> while any(a.count(x) > 1 for x in a):
...     for x in a:
...         if a.count(x) > 1:
...             x = a.pop(x)
...             break
...
>>> a
[0, 1, 2, 3, 4]

The generally recommended way of doing this is of course:

a = list(set(a))

5 Comments

This is not constant space. .pop is a linear-space operation... anyway, note the list is sorted. This can be done in linear time and constant space. It's a classic interview question. Note also, list(set(a)) does not maintain order...
Link to info on list.pop being linear space? That makes no sense to me -- it'd of course be linear time since you'd need to repack the array, but does it copy the entire array on the back end too? If so, gross.
hmmmm actually now that I think about it, you are probably right. It might re-allocate the buffer, I'm actually not sure when you are removing... in any case, the point of this question it to take advantage of the fact that it is sorted.
here's the pop implementation. I believe for index not at the end, it does this unfortunately I think creates a temporary auxiliary buffer? Not entirely sure by reading the implementation Basically, it calls del a[ilow:ihigh] under the hood, which I think is linear space...
Again, not entirely sure, if any C programmers want to wade through the implementation of list_ass_slice by all means ... on second thought, I think in this case it might only allocate an extra buffer of size 1... not sure. Don't really feel like wading through it right now on my phone.
0
def duplicate_remover(input_array):
    for item in input_array:
        while input_array.count(item) > 1:
            input_array.remove(item)
    return input_array

my_array = [0,0,0,0,0,1,1,2,2,2,3,3,3,4,5,6]
print(duplicate_remover(my_array))

1 Comment

I'm not entirely sure this is constant space, I believe list.remove might use an auxiliary buffer underneath the hood, it calls list_ass_slice. In any case this really isn't the way to solve this, it can be done in linear time and constant space, this is quadratic time and I'm not sure if it's constant space either

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.