Find Duplicate Number Problem

Find Duplicate Number Problem — ExecCode Easy DSA Practice

Solve the Find Duplicate Number problem on ExecCode. Free online easy DSA practice in Hashing. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given an array nums of n + 1 integers where every value lies in the range [1, n], there must be exactly one repeated value. Find that duplicate without modifying the array and using only constant extra space, by treating the array as a functional graph and detecting the cycle with Floyd's algorithm.

Examples

Input nums = [1, 3, 4, 2, 2]; Output 2. Input nums = [3, 1, 3, 4, 2]; Output 3. Input nums = [2, 2, 2, 2, 2]; Output 2

Constraints

1 <= n <= 10^5 nums.length == n + 1 1 <= nums[i] <= n Exactly one value repeats one or more times.

Practice Find Duplicate Number free on ExecCode. Browse DSA problems, topic map, and placement guides.