AAMD·DSASDE-1Online Assessment
First Duplicate in O(n) Time, O(1) Space
Problem
Given an array where values are in [1, n], find the first value that appears twice using O(1) extra space.
Example
[2,3,3,1,5,2] -> 3
Constraints
- Values in range [1, n]
Approach
Index-as-hash: negate nums[abs(v)-1]; first already-negative hit is the duplicate.
added 6 days ago