-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_duplicates.py
48 lines (34 loc) · 1014 Bytes
/
find_duplicates.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
from searches.binary_search import binary_search
def duplicates_linear(arr1, arr2):
arr1_seen = set(arr1)
output = []
for element in arr2:
if element in arr1_seen:
output.append(element)
return output
def duplicates_pre_sorted(arr1, arr2):
output = []
ptr_1 = 0
ptr_2 = 0
while ptr_1 < len(arr1) and ptr_2 < len(arr2):
if arr2[ptr_2] == arr1[ptr_1]:
output.append(arr1[ptr_1])
ptr_1 += 1
ptr_2 += 1
elif arr2[ptr_2] > arr1[ptr_1]:
ptr_1 += 1
else:
ptr_2 += 1
return output
def duplicates_bin_search(arr1, arr2):
"""
Find duplicates in 2 sets, where one is much larger than the other.
"""
# if arr1 is greater, swap them
if len(arr2) < len(arr1):
arr1, arr2 = arr2, arr1
output = []
for element in arr1:
if binary_search(arr2, element) >= 0:
output.append(element)
return output