6-line c++ solution


  • 0
    Y
     int findDuplicate(vector<int>& nums) {
            for (int i:nums){
                while (i!=nums[i]){
                    int temp = nums[i];
                    swap(i,nums[i]);
                    i = temp;                  
                }
                if (i==nums[i]) return i;
            }
     }
    

Log in to reply
 

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