Skip to content

Instantly share code, notes, and snippets.

@raja-ashok
Last active April 10, 2018 09:33
Show Gist options
  • Save raja-ashok/04fb206a39188acea1dece8e79132fdb to your computer and use it in GitHub Desktop.
Save raja-ashok/04fb206a39188acea1dece8e79132fdb to your computer and use it in GitHub Desktop.
Find duplicates in an integer list using python
#!/usr/bin/env python
from itertools import groupby
myList = [1, 2, 1, 3, 1]
for x, y in groupby(sorted(myList)):
# groupby groups by consecutive elements which are similar
# x is the element, y is the list which contains the elements occurence
dup_list = list(y)
print 'x : ' + str(x) + ', y: ' + str(list(dup_list))
if (len(list(dup_list)) > 1):
print str(x) + ' is present in the list ' + str(len(dup_list)) + ' times'
x : 1, y: [1, 1, 1]
1 is present in the list 3 times
x : 2, y: [2]
x : 3, y: [3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment