Problem
Given an integer arraynums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Examples
Constraints
- 1 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 217 with 12 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 217 # by problem number
lcpy gen -s contains_duplicate # by problem name
nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Input: nums = [1,2,3,1]
Output: true
Input: nums = [1,2,3,4]
Output: false
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
class Solution:
# Time: O(n)
# Space: O(n)
def contains_duplicate(self, nums: list[int]) -> bool:
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False
| Time | Space |
|---|---|
| O(n) | O(n) |