forked from ruppysuppy/Daily-Coding-Problem-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path164.py
More file actions
30 lines (21 loc) · 580 Bytes
/
164.py
File metadata and controls
30 lines (21 loc) · 580 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
"""
Problem:
You are given an array of length n + 1 whose elements belong to the set {1, 2, ..., n}.
By the pigeonhole principle, there must be a duplicate. Find it in linear time and
space.
"""
from typing import List
def find_duplicate(arr: List[int]) -> int:
seen_numbers = set()
for num in arr:
if num in seen_numbers:
return num
seen_numbers.add(num)
if __name__ == "__main__":
print(find_duplicate([1, 2, 4, 6, 5, 3, 2]))
print(find_duplicate([3, 1, 4, 2, 3]))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""