Skip to content

Commit 47f79af

Browse files
authored
Create 287. Find the Duplicate Number1 (#438)
2 parents 264c519 + 8dd4cac commit 47f79af

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

287. Find the Duplicate Number1

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int findDuplicate(vector<int>& nums) {
4+
int ind = 0;
5+
6+
// sort the vector
7+
sort(nums.begin(),nums.end());
8+
for(int i = 0; i < nums.size() - 1; i++)
9+
{
10+
// if two consecutive elements are equal
11+
// you have find a duplicate
12+
// break the loop
13+
if(nums[i] == nums[i+1])
14+
{
15+
ind = nums[i];
16+
break;
17+
}
18+
}
19+
// return duplicate value
20+
return ind;
21+
}
22+
// for github repository link go to my profile.
23+
};

0 commit comments

Comments
 (0)