3

How do I find duplicates in the list of lists in < n^2 in python? I cannot use a dictionary to do it in linear time as if I did with all standard types. I can only think of the following solution:

arr = [[1,2], [1,2,4], [1,2], [5,6], [8], [8]]
unique_arr = []
dups = []

for item in arr:
    for item2 in unique_arr:
         if (item == item2).all():
            dups.append(item)
            continue
    unique_arr.append(item)

expected result for dups is [[1,2], [8]]

Thanks

YohanRoth
  • 3,153
  • 4
  • 31
  • 58

4 Answers4

4

One possible solution with collections.Counter:

arr = [[1,2], [1,2,4], [1,2], [5,6], [8], [8]]

from collections import Counter

print([[*i] for i,c in Counter(map(tuple, arr)).items() if c > 1])

Prints:

[[1, 2], [8]]

OR:

Version with itertools.groupby and sorted:

from itertools import groupby
print([v for v, g in groupby(sorted(arr, key=len)) if any(i > 0 for i, _ in enumerate(g))])

Prints:

[[8], [1, 2]]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

I don't see why you would need to iterate through the internal lists... You can simply iterate the outer list.

arr = [[1,2], [1,2,4], [1,2], [5,6], [8], [8]]
unique_arr = []
dups = []

for item in arr:
    if item not in unique_arr:
        unique_arr.append(item)
    else:
        unique_arr.append(item)
jdub
  • 1
  • I think "not in" functionality is implemented as a loop too. So it is the same stuff – YohanRoth Jul 29 '19 at 20:02
  • 1
    This still has the same complexity, just way faster because the list comparison is probably in C instead of python, and using an algorithm that is correct – Cireo Jul 29 '19 at 20:02
  • @Cireo oh I see, sure. But the actual time complexity is the same, while I am looking for smth < n^2 – YohanRoth Jul 29 '19 at 20:04
0

Here is one more solution for you:

arr = [[1,2], [1,2,4], [1,2], [5,6], [8], [8]]

dic = {}
dups = []

for ele in arr:
    try:
        if dic[str(ele)] is 1:
            dups.append(ele)
    except:
        dic[str(ele)] = 1


print(dups)

output:

[[1, 2], [8]]
Er. Harsh Rathore
  • 758
  • 1
  • 7
  • 21
0

While you can't use lists as dictionary keys, you can use tuples.

arr = [[1,2], [1,2,4], [1,2], [5,6], [8], [8]]
dups = []
found = set()
for item in arr:
    tup = tuple(item)
    if tup in found:
        dups.append(list(tup))
    found.add(tup)
print(dups)
Malcolm
  • 461
  • 2
  • 10