-1

How can I find the index of the duplicate element in the array?

mylist = [1,2,3,4,5,6,7,8,9,10,1,2]

How can I find the index number of 1 and 2 repeated array elements here? So, not the first duplicate element, I want to find and delete the trailing duplicate element from the array. 1 2 values at the end. Thanks for your responses.

Apo1
  • 1
  • 1
  • 5
  • If you want to get unique values you can use ```set()```. ```myList = list(set(myList))``` will return a list with unique values. – Arthur Aug 08 '22 at 16:03
  • 2
    `list(dict.fromkeys(mylist))`, if you need to ensure their order in the initial list. – Mechanic Pig Aug 08 '22 at 16:04
  • 1
    Does this answer your question? [How do I remove duplicates from a list, while preserving order?](https://stackoverflow.com/questions/480214/how-do-i-remove-duplicates-from-a-list-while-preserving-order) – ouroboros1 Aug 08 '22 at 16:20

5 Answers5

1

Single pass remove duplicates:

mylist = [1,2,3,4,5,6,7,8,9,10,1,2]

def remove_duplicates(l):
    seen = {}
    res = []
    for item in l:
        if item not in seen:
            seen[item] = 1
            res.append(item)
    return res

print(remove_duplicates(mylist))

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Which also preserves order:

mylist = [1,10,3,4,5,6,7,8,9,2,1,2]
print(remove_duplicates(mylist))
[1, 10, 3, 4, 5, 6, 7, 8, 9, 2]
Tom McLean
  • 5,583
  • 1
  • 11
  • 36
  • 3
    IMO this is the right approach, but `seen` is a dict where values don't matter. Make it a set instead: `seen = set()` and `seen.add(item)`. – VPfB Aug 08 '22 at 16:28
0

The simplest way to do this (unless you want to log where the duplicates are) is to convert the list to a set.

A set can contain only one instance of each value.

You can do this very easily as shown below.

Note: A set is respresented with curly braces { like a dictionary, but does not have key,value pairs.

mylist = [1,2,3,4,5,6,7,8,9,10,1,2]
myset = set(mylist)
// myset = {1,2,3,4,5,6,7,8,9,10}

EDIT: If the order is important, this method will not work as a set does not store the order.

HPringles
  • 1,042
  • 6
  • 15
  • 2
    Don't do this, if you want to preserve the order. `set()` does *not* preserve order, even if in this example it **looks** like it does. E.g. with `mylist = [1,2,3,4,5,11,7,8,9,10,1,2]`, myset will become: `{1, 2, 3, 4, 5, 7, 8, 9, 10, 11}`. – ouroboros1 Aug 08 '22 at 16:06
  • Ah yes, that does make sense. I'll update my answer to make that clear – HPringles Aug 08 '22 at 16:07
  • 1
    @HSPringles I made an answer that preserves order, its the classic leetcode remove duplicates problem – Tom McLean Aug 08 '22 at 16:20
  • You could check out the latest post below. – Daniel Hao Aug 08 '22 at 17:27
0

You can find duplicates in a cycle, memorizing previously seen values in a set.

mylist = [1,2,3,4,5,6,7,8,9,10,1,2]

myset = set()
indices_of_duplicates = []

for ind, val in enumerate(mylist):
    if val in myset:
        indices_of_duplicates.append(ind)
    else:
        myset.add(val)

Then you can delete duplicate elements with

for ind in reversed(indices_of_duplicates):
    del mylist[ind]

Note that we are deleting elements starting from the end of the list because otherwise we would shift elements and we would have to update the indices we have found.

0

eh, I did this:

a=[1,2,3,4,5,5,6,7,7,8]
b=list(set(a))

tr=0
al=len(a)
bl=len(b)
iz=[]

if al != bl:
    for ai in range (0,al):
        ai0=a[ai]
        bi0=b[ai]
        if ai0 != bi0:
            iz.append(ai)
            b.insert(ai,ai0)
   

then the list iz is all the i values where they don't match, so you just plug those back in. just wanted to share!

-1

If you have to remove duplicates you can use Set()

mylist = [x for x in set([1,2,3,4,5,6,7,8,9,10,1,2])]
Talon
  • 351
  • 1
  • 11
  • Same problem as with other answer, on top of being verbose. This is just: `list(set([1,2,3,4,5,11,7,8,9,10,1,2]))`, which becomes: `[1, 2, 3, 4, 5, 7, 8, 9, 10, 11]`. So, order is again not preserved. – ouroboros1 Aug 08 '22 at 16:09