-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_duplicates_in_array.cpp
More file actions
46 lines (36 loc) · 956 Bytes
/
Copy pathFind_duplicates_in_array.cpp
File metadata and controls
46 lines (36 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
int find_duplicate(const int arr[], int n)
{
//no duplicates can be there if element is only one
if (n <= 1)
return -1;
//we will use loop concept of Linked List, run two pinter at different speed
//if both the pointers pint the same element, then loop is there. (duplicates)
//initialize fast and slow
int slow = arr[0];
int fast = arr[arr[0]];
// loop to enter in the cycle
while (fast != slow)
{
// move one step for slow
slow = arr[slow];
// move two step for fast
fast = arr[arr[fast]];
}
// loop to find entry point of the cycle
fast = 0;
while (slow != fast)
{
slow = arr[slow];
fast = arr[fast];
}
return slow;
}
int main()
{
const int arr[] = { 1, 2, 3, 4, 5, 6, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << find_duplicate(arr , n);
return 0;
}