Skip to content

Commit 54576dc

Browse files
committed
287. Find the Duplicate Number
1 parent 3a6f01e commit 54576dc

File tree

2 files changed

+215
-0
lines changed

2 files changed

+215
-0
lines changed

287. Find the Duplicate Number.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* 1)
2+
- Brute Force Solution
3+
time Complexity = O(nlogn) ->> Sort function take O(nlogn) and for loop take between O(1 to n)
4+
Space Complexity - O(1) -> Constant Space
5+
*/
6+
7+
class Solution
8+
{
9+
public:
10+
int findDuplicate(vector<int> &nums)
11+
{
12+
sort(nums.begin(), nums.end());
13+
int i = 0;
14+
for (i = 0; i < nums.size() - 1; i++)
15+
{
16+
if (nums[i] == nums[i + 1]) // -> if(nums[i]^nums[i+1] == 0)
17+
{
18+
break;
19+
}
20+
}
21+
return nums[i];
22+
}
23+
};
24+
25+
/* 2)
26+
- Bool Method
27+
time Complexity = O(n)
28+
Space Complexity - O(n) -> Because it is creating an extra arr.
29+
*/
30+
// choose Between 2.1 / 2.2
31+
32+
// 2.1 ->
33+
class Solution
34+
{
35+
public:
36+
int findDuplicate(vector<int> &nums)
37+
{
38+
vector<bool> hash(nums.size(), false);
39+
40+
for (int i = 0; i < nums.size(); i++)
41+
{
42+
if (hash[nums[i]])
43+
{
44+
return nums[i];
45+
}
46+
else
47+
{
48+
hash[nums[i]] = true;
49+
}
50+
}
51+
return -1;
52+
}
53+
};
54+
55+
// 2.2 ->
56+
class Solution
57+
{
58+
public:
59+
int findDuplicate(vector<int> &nums)
60+
{
61+
vector<int> arr(nums.size(), 0);
62+
int i = 0;
63+
64+
// storing 0->1 at position accoring to nums, if any no. came twice then there stores 1->2.
65+
for (i = 0; i < nums.size(); i++)
66+
{
67+
arr[nums[i]]++;
68+
}
69+
70+
for (i = 0; i < arr.size(); i++)
71+
{
72+
if (arr[i] > 1) // element occur more than once in nums
73+
{
74+
break;
75+
}
76+
}
77+
78+
return i;
79+
}
80+
};
81+
82+
/* 3)
83+
- Binary Search Method
84+
time Complexity = O(n^2)
85+
Space Complexity - O(1) -> Constant Space
86+
*/
87+
88+
class Solution
89+
{
90+
public:
91+
int findDuplicate(vector<int> &nums)
92+
{
93+
int low = 1, high = nums.size() - 1, cnt;
94+
95+
while (low <= high)
96+
{
97+
int mid = low + (high - low) / 2;
98+
cnt = 0;
99+
// cnt number less than equal to mid
100+
for (int n : nums)
101+
{
102+
if (n <= mid)
103+
++cnt;
104+
}
105+
// binary search on left
106+
if (cnt <= mid)
107+
low = mid + 1;
108+
else
109+
// binary search on right
110+
high = mid - 1;
111+
}
112+
return low;
113+
}
114+
};
115+
116+
117+
/* 4)
118+
- Hash Map
119+
time Complexity = O(n)
120+
Space Complexity - O(n)
121+
*/
122+
class Solution{
123+
public: int findDuplicate_set(int[] nums) {
124+
Set<int> set = new HashSet<>();
125+
int len = nums.size();
126+
for (int i = 0; i < len; i++) {
127+
if (!set.add(nums[i])) {
128+
return nums[i];
129+
}
130+
}
131+
132+
return len;
133+
}
134+
};

287.txt

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Remember these two properties of XOR operator :
2+
3+
(1) If you take xor of a number with 0 ( zero ) , it would return the same number again.
4+
5+
Means , n ^ 0 = n
6+
7+
(2) If you take xor of a number with itself , it would return 0 ( zero ).
8+
9+
Means , n ^ n = 0
10+
11+
Now , Coming to the problem :
12+
13+
Let Input_arr = { 23 , 21 , 24 , 27 , 22 , 27 , 26 , 25 }
14+
15+
Output should be 27 ( because 27 is the duplicate element in the Input_arr ).
16+
Solution :
17+
18+
Step 1 : Find “min” and “max” value in the given array. It will take O(n).
19+
20+
Step 2 : Find XOR of all integers from range “min” to “max” ( inclusive ).
21+
22+
Step 3 : Find XOR of all elements of the given array.
23+
24+
Step 4 : XOR of Step 2 and Step 3 will give the required duplicate number.
25+
26+
Description :
27+
28+
Step1 : min = 21 , max = 27
29+
30+
Step 2 : Step2_result = 21 ^ 22 ^ 23 ^ 24 ^ 25 ^ 26 ^ 27 = 20
31+
32+
Step 3 : Step3_result = 23 ^ 21 ^ 24 ^ 27 ^ 22 ^ 27 ^ 26 ^ 25 = 15
33+
34+
Step 4 : Final_Result = Step2_result ^ Step3_result = 20 ^ 15 = 27
35+
36+
But , How Final_Result calculated the duplicate number ?
37+
38+
Final_Result = ( 21 ^ 22 ^ 23 ^ 24 ^ 25 ^ 26 ^ 27 ) ^ ( 23 ^ 21 ^ 24 ^ 27 ^ 22 ^ 27 ^ 26 ^ 25 )
39+
40+
Now , Remember above two properties : n ^ n = 0 AND n ^ 0 = n
41+
42+
So , here ,
43+
44+
Final_Result = ( 21 ^ 21 ) ^ ( 22 ^ 22 ) ^ ( 23 ^ 23 ) ^ ( 24 ^ 24 ) ^ ( 25 ^ 25 ) ^ ( 26 ^ 26 ) ^ ( 27 ^ 27 ^ 27 )
45+
46+
= 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ ( 27 ^ 0 ) ( property applied )
47+
48+
= 0 ^ 27 ( because we know 0 ^ 0 = 0 )
49+
50+
= 27 ( Required Result )
51+
52+
53+
54+
Q - if the input is [2,2,2,2,2] then this solution is giving the wrong answer.
55+
ans - Duplicate here means an element that occurs exactly twice, while all other elements occur exactly once. Though this will also work if you define duplicate as meaning an element that occurs an even number of times, while all other elements occur an odd number of times.
56+
57+
58+
59+
class Solution {
60+
public:
61+
int findDuplicate(vector<int>& nums){
62+
int check_1=0;
63+
int check_2=0;
64+
int ans;
65+
66+
int min = *min_element(nums.begin(), nums.end());
67+
int max = *max_element(nums.begin(), nums.end());
68+
69+
for(int i=min;i<=max;i++){
70+
check_1 = check_1 ^ i;
71+
}
72+
73+
for(int i=0;i<nums.size();i++){
74+
check_2 = check_2 ^ nums[i];
75+
}
76+
77+
ans = check_1 ^ check_2;
78+
79+
return ans;
80+
}
81+
};

0 commit comments

Comments
 (0)