Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

nums =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Treat the array as a linked list where i -> nums[i]. The duplicate creates a cycle.

Approach

Use Floyd’s cycle detection:

  1. Find intersection point with slow/fast pointers.
  2. Reset one pointer to start and move both by 1 until they meet; meeting value is the duplicate.

Why it works

With n+1 values in range 1..n, the mapping must repeat, forming a cycle whose entry corresponds to the duplicate.

Code
Loading...
Complexity
Time: O(n)
Space: O(1)
Solution
Updated: 2026-02-23

Idea

Treat the array as a linked list where i -> nums[i]. The duplicate creates a cycle.

Approach

Use Floyd’s cycle detection:

  1. Find intersection point with slow/fast pointers.
  2. Reset one pointer to start and move both by 1 until they meet; meeting value is the duplicate.

Why it works

With n+1 values in range 1..n, the mapping must repeat, forming a cycle whose entry corresponds to the duplicate.

Code
Loading...
Complexity
Time: O(n)
Space: O(1)