Contains Duplicate
Given an integer array nums, return true if any value appears at least twice. Return false if every element is distinct.
This is one of the cleanest hash table problems because the idea is simple: scan the array once, keep a set of values you have already seen, and stop immediately when a number shows up again. If you finish the scan without hitting a repeat, then all values were unique.
Budibadu uses this problem to build the habit of using a set for fast lookups instead of wasting time on nested loops. It is a small pattern, but it shows up everywhere.
Algorithm Flow

Recommendation Algorithm Flow for Contains Duplicate - Budibadu
Best Answers
java
import java.util.*;
class Solution {
public boolean contains_duplicate(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int num : nums) {
if (seen.contains(num)) return true;
seen.add(num);
}
return false;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
