# 217. Contains Duplicate > [!META]- Inline Metadata > [tags:: #career, #leetcode/arrays] > [project group:: Neetcode 150] > [difficulty:: easy] > [status:: done] > [link:: https://leetcode.com/problems/contains-duplicate/] > [up:: [[Leetcode MOC]]] ## Problem 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. **Example 1:** **Input:** nums = [1,2,3,1] **Output:** true **Example 2:** **Input:** nums = [1,2,3,4] **Output:** false ## Solution ### Mine #### Python ```python def containsDuplicate(self, nums: List[int]) -> bool: items = {} for num in nums: if items.get(num): return True else: items[num] = 1 return False ``` Another clever solution takes advantage of sets: ```python def containsDuplicate(self, nums: List[int]) -> bool: return len(nums) != len(set(nums)) ``` #### Rust ### LC's ## Things I Learned ## Scratchpad Slow $O(n^2)$ version would be just a nested loop. Let's not do that. The plus side to it is that the space complexity is $O(1)$. ``` array = [1, 2, 3, 1] for i in array: for j in array: if i == j: return True return False ``` Next possibility is sorting, then you can just check each neighbor. While it's a single pass once sorted, adding sorted brings time complexity up to $O(n log n)$. Space complexity is $O(1)$. We can improve this to $O(n)$ time complexity with the tradeoff of making it $O(n)$ space complexity by using a dictionary to keep track of whether we've seen an item before.