RSSregex
RSSregex

Reputation: 169

Extract Duplicates from List After Applying Sort

I am working on code snippet to extract duplicates from a list. I have seen several implementations/solutions on this site. However, I am not getting this line correctly - syntax wise I think. After sorting, compare index(x) with index(x+1). If it is add to the set.

print(set([i for i in a if (a[i] == a[i+1]))

a = [1,2,3,2,1,5,6,5,5,5]
print(a)
print(set(sorted(a)))
# l1[i] == l1[i+1]
print(set([i for i in a if (a[i] == a[i+1]))
print(set([i for i in a if sum([1 for item in a if item == i]) > 1]))

Expected results: {1, 2, 5}

Upvotes: 4

Views: 85

Answers (4)

sameera lakshitha
sameera lakshitha

Reputation: 1971

Suggest a simple solution to find duplicates from List.

>>> a = [1,2,3,2,1,5,6,5,5,5]
>>> a.sort()
>>> set([x for x in a if a.count(x) > 1])

Output: {1, 2, 5}

Upvotes: 0

Albin Paul
Albin Paul

Reputation: 3419

From what I could gather you are trying to implement this logic, this code runs in O(nlogn) time complexity while the code which runs with counter runs in O(n) time complexity means it is faster and cleaner.

a = [1,2,3,2,1,5,6,5,5,5]
a.sort()
print(set([a[i] for i in range(len(a)-1) if (a[i] == a[i+1])]) )

OUTPUT

set([1, 2, 5])

Upvotes: 2

hiro protagonist
hiro protagonist

Reputation: 46849

you could use collections.Counter:

from collections import Counter

a = [1,2,3,2,1,5,6,5,5,5]
c = Counter(a)

res = [n for n, m in c.items() if m > 1]
print(res)  # [1, 2, 5]

this way you iterate once over the list and once over the counter only.

Upvotes: 5

gmds
gmds

Reputation: 19885

How about this instead?

a = [1,2,3,2,1,5,6,5,5,5]
duplicates = set(element for element in a if a.count(element) > 1)
print(duplicates)

Output:

{1, 2, 5}

Upvotes: 1

Related Questions