Simple 4 line C++ solution using common sense!


  • -1
    S
    class Solution {
    public:
        int findDuplicate(vector<int>& nums) {
            for(int i=0;i<nums.size();i++){
                nums[abs(nums[i])]=-nums[abs(nums[i])];
                if(nums[abs(nums[i])] > 0)
                    return abs(nums[i]);
            }
        }
    };
    

  • 0
    F

    You cannot modify the array...


Log in to reply
 

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