O(n) C++ solution


  • 0
    E
    int findDuplicate(vector<int>& nums) {
        int slow = 0;
        int fast = 0;
    
        do{
            slow = nums[slow];
            fast = nums[nums[fast]];
        } while(slow != fast);
        
        int find = 0;
        while(find != slow){
            slow = nums[slow];
            find = nums[find];
        }
        return find;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.