Skip to content

Latest commit

 

History

History
57 lines (41 loc) · 1.22 KB

_287. Find the Duplicate Number.md

File metadata and controls

57 lines (41 loc) · 1.22 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 26, 2024

Last updated : June 26, 2024


Related Topics : Array, Two Pointers, Binary Search, Bit Manipulation

Acceptance Rate : 62.14 %


Solutions

C

/**
 * Thought process
 * Each value will point to another index, and that
 * to another and another 
 * 
 * We're given values [1, n] inclusive of length n+1 so
 * XORing should be (1^2^...^n) ^ repeatVal
 * 
 */


int findDuplicate(int* nums, int numsSize) {
    int fast = nums[0];
    int slow = nums[0];

    do {
        fast = nums[nums[fast]];
        slow = nums[slow];
    } while (fast != slow);

    slow = nums[0];
    while (slow != fast) {
        slow = nums[slow];
        fast = nums[fast];
    }

    return fast;

}