Skip to content

Commit 6d0b022

Browse files
committed
contains duplicate solution
1 parent 0e5ffe5 commit 6d0b022

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

contains-duplicate/yerim01.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Leetcode 217: https://leetcode.com/problems/contains-duplicate/description/
2+
# Goal: Given an array of integers,
3+
# return True if the array has duplicates or return False if all elements are distinct.
4+
# Approach:
5+
# - Use a hash set to track elements we have already seen.
6+
# - Iterate through the array and if the current number already exists in the set, return True.
7+
# - Otherwise, add the current number to the set.
8+
# Time complexity: O(n)
9+
# - We iterate through the array once.
10+
# Space complexity: O(n)
11+
# - We store all elements in the set in the worst case.
12+
13+
class Solution:
14+
def containsDuplicate(self, nums: List[int]) -> bool:
15+
h = set()
16+
17+
for n in nums:
18+
if n not in h:
19+
h.add(n)
20+
else:
21+
return True
22+
return False

0 commit comments

Comments
 (0)