Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions contains-duplicate/yerim01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Leetcode 217: https://leetcode.com/problems/contains-duplicate/description/
# Goal: Given an array of integers,
# return True if the array has duplicates or return False if all elements are distinct.
# Approach:
# - Use a hash set to track elements we have already seen.
# - Iterate through the array and if the current number already exists in the set, return True.
# - Otherwise, add the current number to the set.
# Time complexity: O(n)
# - We iterate through the array once.
# Space complexity: O(n)
# - We store all elements in the set in the worst case.

class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
h = set()

for n in nums:
if n not in h:
h.add(n)
else:
return True
return False