-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathfind_common_items.py
More file actions
34 lines (25 loc) · 835 Bytes
/
find_common_items.py
File metadata and controls
34 lines (25 loc) · 835 Bytes
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
from typing import List, Sequence, TypeVar
ItemType = TypeVar("ItemType")
def find_common_items(
first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType]
) -> List[ItemType]:
"""
Find common items between two arrays.
Time Complexity: Quadratic
Space Complexity: ON)
Optimal time complexity: O(N)
"""
common_items: List[ItemType] = []
# for i in first_sequence:
# for j in second_sequence:
# if i == j and i not in common_items:
# common_items.append(i)
# return common_items
dictToCheck = {}
for item in first_sequence:
dictToCheck[item] = True
for item in second_sequence:
if item in dictToCheck:
common_items.append(item)
dictToCheck.pop(item)
return common_items