KatInterview

287. Find the Duplicate Number

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to find a single duplicate number in an array nums of n + 1 integers. All integers are within the range [1, n]. We must solve this under two critical constraints: we cannot modify the input array, and we must use only constant, O(1), extra space.

Solution

Find the Duplicate Number in Python

class Solution:
    def findDuplicate(self, nums: List[int]) -> int:
        # Phase 1: Find the intersection point of the two runners.
        # This is Floyd's Tortoise and Hare algorithm.
        # The array is treated as a functional graph where index i points to nums[i].
        tortoise = nums[0]
        hare = nums[0]
        while True:
            tortoise = nums[tortoise]
            hare = nums[nums[hare]]
            if tortoise == hare:
                break

        # Phase 2: Find the "entrance" to the cycle.
        # One pointer starts from the beginning of the array (nums[0]),
        # and the other starts from the intersection point.
        ptr1 = nums[0]
        ptr2 = tortoise
        while ptr1 != ptr2:
            ptr1 = nums[ptr1]
            ptr2 = nums[ptr2]
            
        return ptr1

Find the Duplicate Number in Java

class Solution {
    public int findDuplicate(int[] nums) {
        // Phase 1: Find the intersection point of the two runners.
        // This is based on Floyd's Tortoise and Hare algorithm.
        // The array is treated as a functional graph where index i points to nums[i].
        int tortoise = nums[0];
        int hare = nums[0];
        
        do {
            tortoise = nums[tortoise];
            hare = nums[nums[hare]];
        } while (tortoise != hare);
        
        // Phase 2: Find the "entrance" to the cycle.
        // One pointer starts from the beginning of the array (nums[0]),
        // and the other starts from the intersection point.
        int ptr1 = nums[0];
        int ptr2 = tortoise;
        
        // Move both pointers at the same speed. They will meet at the cycle's entrance,
        // which is the duplicate number.
        while (ptr1 != ptr2) {
            ptr1 = nums[ptr1];
            ptr2 = nums[ptr2];
        }
        
        return ptr1;
    }
}

Find the Duplicate Number in C++

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        // Phase 1: Find the intersection point of the two runners.
        // We use the array indices as nodes and values as pointers.
        int tortoise = nums[0];
        int hare = nums[0];
        
        do {
            tortoise = nums[tortoise];
            hare = nums[nums[hare]];
        } while (tortoise != hare);
        
        // Phase 2: Find the "entrance" to the cycle.
        // Reset one pointer to the start of the array.
        int ptr1 = nums[0];
        int ptr2 = tortoise;
        
        // Move both pointers at the same speed. They will meet at the cycle's entrance.
        while (ptr1 != ptr2) {
            ptr1 = nums[ptr1];
            ptr2 = nums[ptr2];
        }
        
        return ptr1;
    }
};

Find the Duplicate Number in TypeScript

function findDuplicate(nums: number[]): number {
    // Phase 1: Find the intersection point of the two runners using Floyd's algorithm.
    // We treat the array as a linked list where the value at an index is the next index to visit.
    let tortoise = nums[0];
    let hare = nums[0];

    do {
        tortoise = nums[tortoise];
        hare = nums[nums[hare]];
    } while (tortoise !== hare);

    // Phase 2: Find the entrance to the cycle.
    // Reset one pointer to the start of the 'list' (value at index 0).
    // The other pointer remains at the intersection point.
    let ptr1 = nums[0];
    let ptr2 = tortoise;

    // Move both pointers one step at a time. They will meet at the cycle's entrance.
    while (ptr1 !== ptr2) {
        ptr1 = nums[ptr1];
        ptr2 = nums[ptr2];
    }

    return ptr1;
}

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.