aboutsummaryrefslogtreecommitdiff
path: root/contains_duplicate.py
diff options
context:
space:
mode:
Diffstat (limited to 'contains_duplicate.py')
-rw-r--r--contains_duplicate.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/contains_duplicate.py b/contains_duplicate.py
new file mode 100644
index 0000000..840a61e
--- /dev/null
+++ b/contains_duplicate.py
@@ -0,0 +1,27 @@
+# 217. Contains Duplicate
+
+"""
+given an integer array 'nums', return true if any value appears at least
+twice in the array, and return false if every element is distinct
+"""
+
+
+class Solution(object):
+ def containsDuplicate(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: bool
+ """
+ vis = {}
+ for i in nums:
+ if i in vis and vis[i] >= 1:
+ return True
+ vis[i] = vis.get(i, 0) + 1
+ return False
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.containsDuplicate(nums=[1, 2, 3, 1]))
+ print(obj.containsDuplicate(nums=[1, 2, 3, 4]))
+ print(obj.containsDuplicate(nums=[1, 1, 1, 3, 3, 4, 3, 2, 4, 2]))