-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindduplicate.java
70 lines (53 loc) · 1.28 KB
/
findduplicate.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package array;
public class findduplicate {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int[] arr = { 0, 1, 2, 3, 4, 5, 6 };
int[] arr = { 1, 3, 3, 4, 2, 2 };
// findduplicate(arr);
// duplicate(arr);
duplicatetwopointer(arr);
}
// solved in O(n) but used O(n) space
public static void findduplicate(int[] arr) {
int[] temp = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
temp[arr[i]] += 1;
}
for (int i = 0; i < temp.length; i++) {
if (temp[i] > 1) {
System.out.println(i);
}
}
}
// more optimised O(n) we change values of array
// but then again restore it
public static void duplicate(int[] arr) {
int n = arr.length;
for (int i = 0; i < arr.length; i++) {
arr[arr[i] % n] += n;
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] / n > 1) {
System.out.println(i + " duplicate");
}
arr[i] = arr[i] % n;
System.out.println(arr[i]);
}
}
// most optimised two pointer approach
public static void duplicatetwopointer(int[] arr) {
int slow = arr[0];
int fast = arr[0];
do {
slow = arr[slow];
fast = arr[arr[fast]];
} while (slow != fast);
fast = arr[0];
while (slow != fast) {
slow = arr[slow];
fast = arr[fast];
}
System.out.println(slow);
}
}