Skip to content

Commit acf063b

Browse files
committed
Solution as on 29-03-2022 6:00 am
1 parent 46482ae commit acf063b

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

0287. Find the Duplicate Number.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,53 @@ class Solution
6363

6464
return ind;
6565
}
66+
};
67+
68+
class Solution {
69+
public:
70+
int findDuplicate(vector<int>& nums) {
71+
int low = 1, high = nums.size() - 1, cnt;
72+
73+
while(low <= high)
74+
{
75+
int mid = low + (high - low) / 2;
76+
cnt = 0;
77+
// cnt number less than equal to mid
78+
for(int n : nums)
79+
{
80+
if(n <= mid)
81+
++cnt;
82+
}
83+
// binary search on left
84+
if(cnt <= mid)
85+
low = mid + 1;
86+
else
87+
// binary search on right
88+
high = mid - 1;
89+
90+
}
91+
return low;
92+
}
93+
// for github repository link go to my profile.
94+
};
95+
96+
class Solution {
97+
public:
98+
int findDuplicate(vector<int>& nums) {
99+
map<int, int> m;
100+
int duplicate = 0;
101+
for(auto i= 0; i<nums.size(); ++i)
102+
++m[nums[i]];
103+
104+
for(auto i : m)
105+
{
106+
if(i.second > 1)
107+
{
108+
duplicate = i.first;
109+
break;
110+
}
111+
}
112+
return duplicate;
113+
}
114+
// for github repository link go to my profile.
66115
};

0 commit comments

Comments
 (0)