Skip to content

Instantly share code, notes, and snippets.

@pratik7368patil
Created May 27, 2020 18:24
Show Gist options
  • Save pratik7368patil/1b5907da1d49a90d672c4ec131652dfe to your computer and use it in GitHub Desktop.
Save pratik7368patil/1b5907da1d49a90d672c4ec131652dfe to your computer and use it in GitHub Desktop.
Find Duplicate Numbers from Array
# we can use dictionary to store key-value pair
# by using this we can slove this problem in O(n) time complexity
# to do that we need to import Counter from collections module
from collections import Counter
def find_duplicate(arr):
dc = Counter(arr) # accoring to python's docs this will take O(n)
for val, count in dc.items(): # this will take O(n)
if count > 1: # looking for an element which occure more 1 time
print(val)
arr = [10,20,10,20,30,40,30,50] # original array
find_duplicate(arr) # this will print each value to new line
# the output will be 10 20 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment