Asked at 12 companies
5 recent
- Bloomberg
- Amazon
- Microsoft
- Meta
- Yahoo
- Netflix
- Capgemini
- Airbnb
- tcs
+2 moreShow fewer
- ZScaler
- SAP
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Counting subarrays with a map of totals
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Caches and duplicate detection: A hash map answers “have I seen this before?” in one step. It's how apps cache results, and how a photo app spots the same picture uploaded twice.
ProblemProblem statementGiven an integer array nums, return true if any value appears more than once in the array, otherwise return false.ReadHide
Problem Description
Given an integer array nums, return true if any value appears more than once in the array, otherwise return false.
Examples
Example 1: AlgoChronicle Custom Gem That's an excellent point! To maintain consistency and accurately reflect the source of the problem, I'll update the link and platform fields in the Contains Duplicate README to reference the NeetCode 150 list.
Here is the updated related_contains_duplicate/README.md.
- Updated Problem README (related_contains_duplicate/README.md) The metadata has been adjusted to use the NeetCode platform and link.
Markdown
dayNumber: 1 title: Contains Duplicate platform: NeetCode 150 link: https://leetcode.com/problems/contains-duplicate/ difficulty: Easy pattern: Hash Table / Set approach: Use a HashSet/Unordered Set to track seen numbers. optimized: O(n) bruteforce: O(n²) space: O(n) date: 2025-10-23 commitHash: [INJECTED BY GITHUB ACTION] tags:
- array
- hash-table
Contains Duplicate (NeetCode: Duplicate Integer)
Platform: NeetCode 150 | Difficulty: Easy | Pattern: Hash Table
Problem Description
Given an integer array nums, return true if any value appears more than once in the array, otherwise return false.
Examples
Example 1: Input: nums = [1, 2, 3, 3] Output: true
Example 2: Input: nums = [1, 2, 3, 4] Output: false
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Use a HashSet/Unordered Set to track seen numbers.
Approaches
1. Brute Force - O(N²)
Idea: Compare every element with every other element. Use nested loops to check if nums[i] == nums[j] where i ne j.
Time: O(N²) Space: O(1)
2. Hash Set (Optimized) - O(N) ⭐
Idea: This is the ideal use case for a Hash Set. Iterate through the array once, storing each number encountered in the set. Before adding a number, check if it already exists. If it does, you've found a duplicate and can immediately return true.
Time: O(N) - Single pass with O(1) average lookup/insertion.
Space: O(N) - To store the set, where N is the number of elements.
Solutions
- 🐍 solution.py - Python implementation
- ☕ solution.java - Java implementation
- ⚡ solution.cpp - C++ implementation
Complexity Analysis
| Approach | Time | Space |
|---|---|---|
| Brute Force | O(N²) | O(1) |
| Hash Set | O(N) | O(N) |
Winner: Hash Set ⭐
Solution code
from typing import List
class Solution:
# 1. Brute Force Approach: Time O(n^2), Space O(1)
# Compares every pair of elements.
def containsDuplicate_brute(self, nums: List[int]) -> bool:
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] == nums[j]:
return True
return False
# 2. Optimized Approach: Time O(n), Space O(n) - Set
# Uses a set for O(1) average time complexity lookups.
def containsDuplicate_optimized(self, nums: List[int]) -> bool:
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False
# Pythonic One-Liner (O(n) Time/Space)
def containsDuplicate_pythonic(self, nums: List[int]) -> bool:
return len(nums) != len(set(nums))
# Default entry point (using the optimized method)
def containsDuplicate(self, nums: List[int]) -> bool:
return self.containsDuplicate_optimized(nums)Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language