/
find_dup.py
51 lines (40 loc) · 1.2 KB
/
find_dup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'''
Find duplicates in an array of int
'''
import merge_sort
def find_dup_via_sort(array):
"""
:type array: list of int
:return the smallest duplicates
"""
sorted_array = merge_sort.merge_sort(array)
for i in range(len(sorted_array)):
if i > 0:
if sorted_array[i - 1] == sorted_array[i]: return sorted_array[i]
return None
def find_dup(array):
"""
:type array: list of positive integers
:return list of all duplicates
"""
checking_array = []
dups = []
# check if list is empty
if len(array)<1:
return None
# first find the largest element
max_element = array[0]
for element in array:
max_element = max(max_element, element)
# next make an array of 0s with size as largest as the largest element in given array
for i in range(max_element + 1):
checking_array.append(0)
# then map each element to the new array with index = value of the element
for j in array:
if j < 0: raise ValueError
checking_array[j] += 1
# now check if there are duplicates
for k in range(len(checking_array)):
if checking_array[k] > 1:
dups.append(k)
return dups