-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontains_duplicate.py
More file actions
36 lines (27 loc) · 1.34 KB
/
contains_duplicate.py
File metadata and controls
36 lines (27 loc) · 1.34 KB
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
31
32
33
34
35
36
# https://leetcode.com/problems/contains-duplicate/
# Given an array of integers, find if the array contains any duplicates.
# Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
# has_key() vs in : http://stackoverflow.com/questions/1323410/has-key-or-in
# If you had implement this solution using has_keys() and bumped into an error,thats
# because has_Keys() has been deprecated in newer versions of python , using "in" is more pythonic
# Solution
# Create a dictionary or map datastructure , keep adding the numbers into the dictionary, bail out when you hit a dupe
class Solution(object):
def containsDuplicate(self, nums):
self.my_dict={}
self.nums = nums
for i in range(len(self.nums)):
#if self.my_dict.has_Key(self.nums[i]) == True:
if self.nums[i] in self.my_dict:
return True
else:
self.my_dict[nums[i]] = 0
return False
# TESTING
my_solution = Solution()
numbers_test_1 = [3,5,7,8,9,23,12]
print my_solution.containsDuplicate(numbers_test_1)
numbers_test_2 = [3,5,7,8,9,23,12,3]
print my_solution.containsDuplicate(numbers_test_2)
# COMPLEXITY
# Well, this takes a single pass of the nums list , the solution is O(n) but takes additional space for the dictionary