π NeetCode 150: Contains Duplicate (01. Duplicate Integer)
- NeetCode Link: Contains Duplicate - NeetCode
- LeetCode Link: LeetCode 217 - Contains Duplicate
- Difficulty:
Easy - Category:
Arrays & Hashing - Recommended Complexity: Time: | Space:
- C# Project File:
01.Duplicate-Integer.csproj - C# Source Code:
Program.cs - Shared Runner Library:
DSA.Common
π Problem Statement
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: trueExample 2:
Input: nums = [1, 2, 3, 4]
Output: falseConstraints
- $0 \le \text{nums.length} \le 10
π‘ NeetCode Hints & Strategy
TIP
- Hint 1: A brute force solution would be to check every element against every other element in the array. This would be an solution. Can you think of a better way?
- Hint 2: Is there a way to check if an element is a duplicate without comparing it to every other element? Maybe thereβs a data structure that is useful here.
- Hint 3: We can use a hash data structure like a hash set or hash map to store elements weβve already seen. This will allow us to check if an element is a duplicate in constant time.
ποΈ Space to Practice
π Step-by-Step Self-Solving Framework
- Understand Goal: Return
trueif duplicate exists,falseif all numbers unique. - Evaluate Constraints: brute force will TLE on large arrays.
- Select Data Structure: Hash Set allows insertion & lookup.
- Draft Pseudocode:
seen = empty set for each x in nums: if x is in seen: return true add x to seen return false
π» My C# Code Scratchpad
public static bool UserSolution(int[] nums)
{
// Write your solution here!
return false;
}π§ͺ Test Cases & Benchmark Matrix
| Case # | Input nums | Expected Output | Type | Goal Time | Goal Memory |
|---|---|---|---|---|---|
| 1 | [1, 2, 3, 3] | true | Standard Duplicate | Minimal | |
| 2 | [1, 2, 3, 4] | false | All Unique | set alloc | |
| 3 | [1, 1, 1, 3, 3, 4] | true | Multiple Duplicates | Early return | |
| 4 | [] | false | Empty Array | ||
| 5 | [7] | false | Single Element | Minimal | |
| 6 | [-1, -2, -3, -1] | true | Negative Numbers | Minimal |
π‘ Multi-Solution Analysis (Deep Step-by-Step Explanations)
Approach 1: HashSet (Optimal Solution) β
Intuition & Concept
A Hash Set maintains unique elements with average lookup and insertion time. As we iterate through nums, we query the set to see if the element was seen previously.
Step-by-Step Solving Logic
- Initialize Set: Create
HashSet<int> seen. - Iterate Array: Loop over each integer
numinnums. - Add & Check: In C#,
seen.Add(num)returnsfalseifnumis already present inseen. - Early Exit: If
seen.Add(num)returnsfalse, returntrueimmediately. - Completion: If loop completes, return
false.
C# Source Code
public static bool Reference_HashSet(int[] nums)
{
HashSet<int> seen = new HashSet<int>();
foreach (int num in nums)
{
if (!seen.Add(num)) return true;
}
return false;
}Complexity Analysis
- Time Complexity: β Single pass; average set insertion.
- Space Complexity: β Up to elements stored in set in worst case.
Approach 2: Sorting (Alternative Solution)
Intuition & Concept
Sorting rearranges elements into ascending order. Any duplicate values will sit adjacent to each other.
Step-by-Step Solving Logic
- Base Case: If
nums.Length <= 1, returnfalse. - Sort: Call
Array.Sort(nums). - Compare Neighbors: Loop from
1tonums.Length - 1. Ifnums[i] == nums[i - 1], returntrue. - Completion: Return
false.
C# Source Code
public static bool Reference_Sorting(int[] nums)
{
if (nums.Length <= 1) return false;
Array.Sort(nums);
for (int i = 1; i < nums.Length; i++)
{
if (nums[i] == nums[i - 1]) return true;
}
return false;
}Complexity Analysis
- Time Complexity: β Sorting dominates.
- Space Complexity: auxiliary space if sorted in-place.
Approach 3: Brute Force (Naive Baseline)
Intuition & Concept
Compare all pairs where .
Step-by-Step Solving Logic
- Loop from to .
- Loop from to .
- If
nums[i] == nums[j], returntrue. - Return
false.
C# Source Code
public static bool Reference_BruteForce(int[] nums)
{
for (int i = 0; i < nums.Length; i++)
{
for (int j = i + 1; j < nums.Length; j++)
{
if (nums[i] == nums[j]) return true;
}
}
return false;
}Complexity Analysis
- Time Complexity: β comparisons.
- Space Complexity: β Zero extra dynamic allocation.
π οΈ C# Tips & Common Pitfalls
- C#
HashSet.Add(x)Method:seen.Add(x)returnsbool(falseif element already existed).
- Memory Overhead:
HashSet<int>has memory overhead per entry compared to arrays. For ultra-low memory environments, sorting is space.
- Shared Benchmarking:
- Use
DSA.Common.DsaTestRunnerto measure execution time (ms) and thread memory allocations (Bytes) automatically!
- Use