Contains Duplicate
FreeArrays & hashingEasy2 of 75
The problem
Decide whether an integer array contains any repeated value. Return true when two different positions hold the same number.
Example
[7, 2, 7, 4] → true; [7, 2, 4] → false
Need a hint?
Only membership matters; the earlier position is irrelevant.
Write pseudocode, trace the example, or note an edge case. This scratchpad does not run code.
Notes stay in this browser when storage is available.
Read the solution approach
Maintain a set of visited values. If the next number is already in the set, return true. Otherwise insert it and continue. Return false after processing the entire array. An empty array has no duplicate.
Complexity
O(n) expected time and O(n) space.
Before moving on, explain why the algorithm is correct and trace a boundary case without looking at the approach.