We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0e5ffe5 commit 6d0b022Copy full SHA for 6d0b022
1 file changed
contains-duplicate/yerim01.py
@@ -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