Skip to content

Instantly share code, notes, and snippets.

@jonpemby
Last active March 15, 2019 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonpemby/7382ce4b72f827ebccc4266f5aabf0e6 to your computer and use it in GitHub Desktop.
Save jonpemby/7382ce4b72f827ebccc4266f5aabf0e6 to your computer and use it in GitHub Desktop.
Find duplicate integers in Python
def find_duplicates(nums):
sorted_nums = sorted(nums)
last = None
dups = []
for n in sorted_nums:
if last and n == last:
try:
last_dup = dups[-1]
except IndexError:
last_dup = n + 1
if last_dup != n:
dups.append(n)
last = n
return dups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment