Intuition
The key insight is to use a HashSet to keep track of elements we've already seen as we iterate through the array.
- Iterate Through Array: Process each element one by one from left to right.
- Check HashSet: Before processing, check if the current element already exists in our HashSet.
- Duplicate Found: If element exists in HashSet, we found a duplicate - return true immediately.
- Add to HashSet: If element doesn't exist, add it to the HashSet and continue.
- No Duplicates: If we finish the entire array without finding duplicates, return false.
Step-by-Step Code
Live Visualization
Input Array:
HashSet Contents:
HashSet is empty
Algorithm States
Status: Ready
Current Index (i): N/A
Current Element: N/A
HashSet Size: 0
Duplicate Found: false