Last active
March 15, 2019 13:51
-
-
Save jonpemby/7382ce4b72f827ebccc4266f5aabf0e6 to your computer and use it in GitHub Desktop.
Find duplicate integers in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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