-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathfind_common_items.py
More file actions
42 lines (31 loc) · 1006 Bytes
/
find_common_items.py
File metadata and controls
42 lines (31 loc) · 1006 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
35
36
37
38
39
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:
# """
# 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
def find_common_items(
first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType]) -> List[ItemType]:
"""
Find common items between two arrays.
Time Complexity: O(n+m)
Space Complexity: O(m+k)
Optimal time complexity: O(n+m)
"""
set_1 = set(first_sequence)
set_2 = []
for seq in set_1:
if seq in second_sequence:
set_2.append(seq)
return set_2