Contains Duplicate in Python and Java
In this tutorial, we will learn LeetCode #217: Contains Duplicate in very simple language. We will understand the idea step by step, see custom examples, and write complete code in Python and Java.
What Is the Contains Duplicate Problem?
This problem asks us to solve a common coding interview task using the given input. The goal is to return the correct result without using a slow brute force method.
For this problem, we will use hash set because it gives a clean and optimized solution.
Example input: nums = [4, 9, 1, 6, 9] Expected result: Output: true Explanation: The number 9 appears twice.
Beginner-Friendly Idea
The main idea is to avoid trying every possible answer blindly. Instead, we keep useful information while reading the input and use that information to make the next decision.
At each step, ask: “What do I already know, and how does the current value change my answer?”
Using our example: nums = [4, 9, 1, 6, 9] Approach: hash set We update variables step by step until we reach: Output: true
Why Do We Use This Approach?
A direct brute force solution is usually easier to think about, but it can become slow when the input is large. The optimized approach keeps only the important state and avoids repeated work.
That is why hash set is useful for this problem.
Step-by-Step Explanation
Let us dry run the algorithm using a custom example.
Step 1
Start with an empty set. This set stores numbers we have already seen.
seen = {}
Step 2
Read the first number 4.
4 is not in seen.
So we add it.
seen = {4}
Step 3
Read the next important number 9.
9 is not in seen.
So we add it.
seen = {4, 9}
Step 4
Later, we see 9 again.
9 is already in seen.
That means we found a duplicate, so we return true.Important Code Logic
The most important part is updating the algorithm state after reading each useful value. This is where the answer becomes better step by step.
Think like this: old_state = what we knew before current_value = value we are checking now new_state = updated result after using current_value For our example, the final state gives: Output: true
Example 1
Input: nums = [4, 9, 1, 6, 9] Output: Output: true Explanation: The number 9 appears twice.
Example 2
Input: nums = [12, 3, 7, 12, 9] Output: true Explanation: The number 12 appears more than once.
Python Code
Here is the complete Python solution for LeetCode #217.
class DuplicateNumberChecker:
def contains_duplicate(self, nums):
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False
checker = DuplicateNumberChecker()
print(checker.contains_duplicate([4, 9, 1, 6, 9])) # Output: True
print(checker.contains_duplicate([5, 1, 8, 3])) # Output: FalseJava Code
Here is the complete Java solution for LeetCode #217.
import java.util.HashSet;
import java.util.Set;
class DuplicateNumberChecker {
public boolean containsDuplicate(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int num : nums) {
if (seen.contains(num)) {
return true;
}
seen.add(num);
}
return false;
}
public static void main(String[] args) {
DuplicateNumberChecker checker = new DuplicateNumberChecker();
System.out.println(checker.containsDuplicate(new int[] {4, 9, 1, 6, 9})); // Output: true
System.out.println(checker.containsDuplicate(new int[] {5, 1, 8, 3})); // Output: false
}
}Time and Space Complexity
Time Complexity: O(n)
The time complexity depends on how many values the algorithm needs to process and whether it uses sorting, binary search, heap, or traversal.
Space Complexity: O(n)
The extra space is used for the variables or data structures needed by the optimized approach.
Final Summary
LeetCode #217: Contains Duplicate becomes easier when we break it into small steps. First understand what the problem asks, then track the important state, dry run with an example, and finally write the code.