Skip to content

Commit 4e68d15

Browse files
committed
Contains Duplicate
1 parent a114c48 commit 4e68d15

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Arrays/217-Contains-duplicate.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''Leetcode - https://leetcode.com/problems/contains-duplicate/'''
2+
'''
3+
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.
4+
5+
Input: nums = [1,2,3,1]
6+
Output: true
7+
'''
8+
9+
# Solution1
10+
def containsDuplicate(nums):
11+
for i in range(len(nums)):
12+
for j in range(i+1, len(nums)):
13+
if nums[i] == nums[j]:
14+
return True
15+
return False
16+
17+
# T:O(N^2)
18+
# S:O(1)
19+
20+
21+
# Solution2
22+
def containsDuplicate(nums):
23+
hashset = set()
24+
25+
for i in len(nums):
26+
if nums[i] in hashset:
27+
return True
28+
hashset.add(nums[i])
29+
return False
30+
31+
# T: O(N)
32+
# S: O(N)
33+
34+
# Solution3
35+
def containsDuplicate(nums):
36+
nums.sort()
37+
38+
for i in len(1, nums):
39+
if nums[i] == nums[i-1]:
40+
return True
41+
return False
42+
43+
# T: O(nlogN)
44+
# S: O(1)

0 commit comments

Comments
 (0)