Skip to content

Contains Duplicate

Given an integer array nums, return true if any value appears at least twice, and false if every element is distinct.

InputOutputExplanation
[1, 2, 3, 1]true1 appears at indices 0 and 3
[1, 2, 3, 4]falseAll elements are distinct
[1, 1, 1, 3, 3, 4, 3, 2, 4, 2]trueMultiple duplicates present
  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
ApproachTimeSpaceBest When
Hash SetO(n)O(n)General case — fast lookup, stop early on first duplicate
SortingO(n log n)O(1)Memory is constrained and you can afford extra time
  • Pick Hash Set for interviews — O(n) with early exit is the expected answer.
  • Pick Sorting if you need to understand the space trade-off or the interviewer bans extra memory.
Best For Speed
Hash Set
O(n) time · O(n) space
No Extra Memory
Sorting
O(n log n) time · O(1) space
★ Recommended

Scan the array once. For each number, check if it is already in the set. If yes, return true immediately. Otherwise add it to the set and continue. A hash set gives O(1) average lookup and insert.

⏱ Time O(n) Single pass, early exit 💾 Space O(n) Set stores up to n elements

Input: [1, 2, 3, 1]

StepnumIn seen?seen after
11No&#123;1&#125;
22No&#123;1, 2&#125;
33No&#123;1, 2, 3&#125;
41Yes → return true

Animated walkthrough

How the hash set scan detects a duplicate

Use Next or Autoplay to see each number checked against the set and either stored or flagged as a duplicate.

Step 1 of 5
Waiting to begin...
Array state
Memory / lookup state

function containsDuplicate(nums):
seen = {}
for num in nums:
if num in seen:
return true
seen.add(num)
return false
contains_duplicate_hash_set.py
from typing import List
def contains_duplicate(nums: List[int]) -> bool:
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False
✓ Simple

Sort the array first. After sorting, any duplicate values must be adjacent. A single pass then detects any pair of equal neighbours.

⏱ Time O(n log n) Dominated by sort 💾 Space O(1) In-place sort, no extra structure

Input: [3, 1, 3, 2]

After sorting: [1, 3, 3, 2] → wait: [1, 2, 3, 3]

inums[i]nums[i-1]Equal?
121No
232No
333Yes → return true
function containsDuplicate(nums):
sort(nums)
for i from 1 to len(nums) - 1:
if nums[i] == nums[i - 1]:
return true
return false
contains_duplicate_sorting.py
from typing import List
def contains_duplicate(nums: List[int]) -> bool:
nums.sort()
for i in range(1, len(nums)):
if nums[i] == nums[i - 1]:
return True
return False
print(contains_duplicate([1, 2, 3, 1])) # True
print(contains_duplicate([1, 2, 3, 4])) # False
✓ Simple

A third approach demonstrating an alternative technique for solving this problem. This implementation optimizes either time or space complexity compared to the previous approaches.

⏱ Time O(n) Optimized complexity 💾 Space O(1) Efficient space usage
function approach3():
// Implementation approach goes here
contains_duplicate_space_optimized.py
"""
Solution for Contains Duplicate
"""
def solve():
"""Implementation for approach 3 of Contains Duplicate"""
pass
if __name__ == "__main__":
print("Solution ready")