-
Notifications
You must be signed in to change notification settings - Fork 0
/
P10.java
33 lines (28 loc) · 834 Bytes
/
P10.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
import java.io.*;
import java.util.*;
import java.util.ArrayList;
public class P10 {
public int using_space(ArrayList<Integer> arr) {
HashSet<Integer> set = new HashSet<>();
for (int i : arr)
if (!set.add(i))
return i;
return -1;
}
public static int without_space(ArrayList<Integer> arr, int n) {
// Cycle Method
for (int i = 0; i < arr.size(); i++) {
int temp = (int) Math.abs(arr.get(i));
if (arr.get(temp - 1) < 0)
return temp;
else {
arr.set(temp - 1, arr.get(temp - 1) * -1);
}
}
return -1;
}
public static int findDuplicate(ArrayList<Integer> arr, int n) {
// return using_space(arr);
return without_space(arr, n);
}
}