Remove Duplicates


Hi, i wanted to know if is possible solve this exercise using two indexes "i" and "j" obviously my code is not correct and i found another way but i propose it the same to make an idea.
Thanks in advance

def remove_duplicates(x):
    for i and j in x:
        if i == j:
    return x


Pretty much any solution you're able to carry out manually will work in code, you just need to be able to describe it in simple instructions, so it can't for example rely on intuition, because that's not something we know how to implement.

You haven't described what should happen with i and j as your loop runs, so there's no way I could say if that something can be done or not. Perhaps you just want to use two loops instead, or update the other variable based on the other during each iteration


Ok you're right, thank you for the answer, i was imagining to use the double index like in a matrix but i concluded that is not the better way =)


Yes but the easier the better right :smiley:


And my code is the below. FYI.

def remove_duplicates(alist):
    if (len(alist)==0): return rlist #if list is empty,return empty directly
    for i in range(1,len(alist)):
        if (alist[i]!=temp) and  alist[i] not in rlist:
    return rlist

print remove_duplicates(["a","b",3.4,6,7,"a"])


It seems your algorithm is wrong, because you should not compare each element to another, just need compare element with the prior one.


You are doing that though, you do compare to all elements that you've added so far

That isn't sufficient, you would end up with duplicates for [1, 2, 1] since the 1's are not adjacent


For your first comment: what is I pointed out is that there's no need to compare the element to the element who's index is greater

For your second comment: Yes, you are right. The situation should be considered. And Indeed, I considered that. Please refer to my code in the previous post.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.