Arrays
Episode 3 Contains Duplicate
▶ This video is coming to YouTube soon.
LeetCode 217, Contains Duplicate, solved on screen. Three examples show what counts: a repeat anywhere in the array, no repeat at all, and an array full of repeats where one is already enough. Then we watch the brute force compare every pair, read its code line by line, and see why it's O(n²). Finally, one pass with a HashSet answers in O(n): Java's add() returns false when a number is already there.
What you'll learn
- What Contains Duplicate asks, through three worked examples
- The brute force: every pair, O(n²) time, O(1) space
- The key idea: a duplicate is a number you've already seen
- The HashSet solution: O(n) time, O(n) space
- Why HashSet.add() returning false is all the check you need
Chapters
- 0:00The problem
- 0:13Examples
- 0:45Brute force, visualized
- 1:30Brute force: the code
- 1:44Brute force, line by line
- 2:19HashSet: the idea
- 2:36HashSet, visualized
- 3:09HashSet: the code
- 3:24HashSet, line by line
- 3:59Comparing the approaches