Skip to content

Latest commit

 

History

History
23 lines (17 loc) · 596 Bytes

17.md

File metadata and controls

23 lines (17 loc) · 596 Bytes

Find the Duplicate

 int findDuplicate(vector<int>& nums) {
        
                //O(n^2)

        for(int i=0;i<nums.size()-1;i++)
            for(int j=i+1;j<nums.size();j++)
                if(nums[i]==nums[j])
                    return nums[i];
        return -1;


                //O(nlogn)
                
        sort(nums.begin(),nums.end());
        for(int i=0;i<nums.size()-1;i++)
            if(nums[i]==nums[i+1])
                return nums[i];
        return -1;
    }