Given an integer array nums
, determine if any value appears at least twice in the array. Return true
if any value appears at least twice in the array, and return false
if every element is distinct.
For example:
nums = [1,2,3,1]
should return true
because 1
appears twice.nums = [1,2,3,4]
should return false
because all elements are distinct.nums = [1,1,1,3,3,4,3,2,4,2]
should return true
because 1
, 3
, 4
, and 2
all appear multiple times.What is the most efficient algorithm to solve this problem?
## Contains Duplicate
### Problem Description
Given an integer array `nums`, determine if any value appears at least twice in the array. Return `true` if a duplicate is found, and `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
**Example 3:**
Input: nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2] Output: true
### Brute Force Solution
A straightforward approach is to iterate through the array and, for each element, check if it exists later in the array. This involves nested loops.
```python
def contains_duplicate_brute_force(nums):
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] == nums[j]:
return True
return False
Using a hash set (or just a set in Python) can significantly improve performance. Iterate through the array and add each element to the set. If an element is already in the set, it means a duplicate has been found.
def contains_duplicate_optimal(nums):
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False
Brute Force Solution:
Optimal Solution:
Brute Force Solution:
Optimal Solution:
seen
set might store all the elements of the array if there are no duplicates.false
because there can be no duplicates.false
.Here's the implementation that handles the first two edge cases:
def contains_duplicate_optimal_edge_cases(nums):
if not nums:
return False
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False