📘 LeetCode 217: Contains Duplicate
📌 LeetCode: 217.Contains-Duplicate
📋 Problem Statement
title: “LeetCode 217: Contains Duplicate — Engineering & Algorithmic Solution Note” problem_number: 217 problem_name: “Contains Duplicate” difficulty: “Easy” category: “dsa_solutions” tags: [leetcode-217, contains-duplicate, hash-set, array, csharp, dotnet9, dsa, problem-solving, leetcode, learning-note]
📘 LeetCode 217: Contains Duplicate — Engineering & Algorithmic Solution Note
📌 Connected Resource Graph
- 🟢 Primary Theory Note: 00_overview_hash_tables_dictionary_hashmap.md
- 🟡 Topic Overview Node: 00_overview_array_contiguous_memory.md
- 🎓 Vietnamese DSA Roadmap: 01_DSA/01_theory_and_patterns/Lộ trình học DSA - Nền tảng thuật toán.md
- 📂 Local Project Companion Files:
- 💻
Program.cs - ⚡
complexity.md - 🚨
tips.md - ✍️
practice.md
- 💻
1. 🎯 Problem Statement & Constraints
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Examples
Example 1:
Input: nums = [1, 2, 3, 1]
Output: true
Explanation: The element 1 occurs at the indices 0 and 3.Example 2:
Input: nums = [1, 2, 3, 4]
Output: false
Explanation: All elements are distinct.Example 3:
Input: nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]
Output: trueConstraints
2. 💡 Optimal Implementation (C# Hash Set)
public static bool ContainsDuplicate(int[] nums)
{
var seen = new HashSet<int>(nums.Length);
foreach (var num in nums)
{
if (!seen.Add(num))
{
return true; // Already exists!
}
}
return false;
}Key Engineering Observations:
HashSet<T>.Add()returnsfalseif the item is already present, allowing single-pass early termination.- Initializing with
new HashSet<int>(nums.Length)prevents costly internal bucket re-allocations for large arrays.
💡 Multi-Solution Analysis
title: “Complexity Analysis — LeetCode 217: Contains Duplicate” tags: [leetcode-217, contains-duplicate, complexity, big-o, hash-set, dsa, problem-solving, leetcode, learning-note]
⚡ Complexity Analysis — LeetCode 217: Contains Duplicate
1. Asymptotic Comparison Matrix
| Approach | Best Time | Average Time | Worst Time | Space Complexity | Notes |
|---|---|---|---|---|---|
| Brute Force (Nested Loops) | TLE on | ||||
| Sorting In-Place | or | Mutates input array | |||
| Hash Set (Optimal) | Industry standard, fast lookup |
2. Deep Dive: Hash Set Tradeoffs
- Time Complexity: average time. In the worst case where duplicate is at the end or no duplicates exist, we iterate times with set additions.
- Space Complexity: additional memory to hold up to distinct integers in the heap.
🛠️ C# Tips & Common Pitfalls
title: “Interview Tips & Traps — LeetCode 217: Contains Duplicate” tags: [leetcode-217, contains-duplicate, interview-tips, edge-cases, dsa, problem-solving, leetcode, learning-note]
🚨 Interview Tips & Traps — LeetCode 217: Contains Duplicate
💡 Edge Cases to Mention to Interviewer
- Array Length 1: Return
falseimmediately since an array with a single element cannot contain duplicates. - All Elements Identical: E.g.
[7, 7, 7, 7]. Loop terminates immediately on index 1. - Negative Numbers & Zero: Sets properly handle sign bits and
0without special cases. - Memory-Constrained Environments:
If the interviewer says: “You cannot allocate additional heap memory”, pivot to In-Place Sorting ( time, space) and check
nums[i] == nums[i-1].