forked from CodeYourFuture/Module-Complexity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_common_items.py
More file actions
28 lines (19 loc) · 824 Bytes
/
find_common_items.py
File metadata and controls
28 lines (19 loc) · 824 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
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:
Space Complexity:
Optimal time complexity:
"""
""" In this case, the time complexity is O(n * m), it will only become O(n^2) when the two arrays have equal lengths. This case easily be avoided by using a set to store the second sequence. When checking for a match in the set using item in second-set, this has a time complexity of O(1).
"""
second_set = set(second_sequence)
common_items = []
for item in first_sequence:
if item in second_set:
common_items.append(item)
return common_items