-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContains Duplicate.py
More file actions
22 lines (15 loc) · 913 Bytes
/
Copy pathContains Duplicate.py
File metadata and controls
22 lines (15 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# https://leetcode.com/problems/contains-duplicate/description/
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
"""
OBJECTIVE: 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.
Time Complexity: O(n) where n = length of nums. set() will iterate nums and save each unique element to itself
Space Complexity: O(k) where k = number of unique elements inside nums. Worst case scenario is having only unique
elements inside of nums. If that happens, all elements will be saved in numsSet.
"""
# Turn list to a set
numsSet = set(nums)
# If set and list doesn't share same size, then return true because nums contains duplicates
if len(numsSet) != len(nums):
return True
return False