Created
May 27, 2020 18:24
-
-
Save pratik7368patil/1b5907da1d49a90d672c4ec131652dfe to your computer and use it in GitHub Desktop.
Find Duplicate Numbers from Array
This file contains hidden or 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
# 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