Created
October 7, 2022 19:15
-
-
Save webber2408/78f9194362fbd7c408574d1c0d02d696 to your computer and use it in GitHub Desktop.
Approach-1 Find Duplicates (Sort and find)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int findDuplicate(vector<int>& nums) { | |
sort(nums.begin(), nums.end()); | |
for (int i = 1; i < nums.size(); i++) { | |
if (nums[i] == nums[i - 1]) | |
return nums[i]; | |
} | |
return -1; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment