forked from akash-coded/C133-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindDuplicate.java
34 lines (30 loc) · 966 Bytes
/
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
public class FindDuplicate {
static void findDuplicateNumber(int[] num) {
for (int i = 0; i < num.length; i++) {
int j = Math.abs(num[i] - 1);
if (num[j] > 0) {
num[j] = -num[j];
} else {
System.out.print(j - 1 + " ");
}
}
}
static void findDuplicateNumber2(int[] num) {
for (int i = 0; i < num.length; i++) {
num[num[i] % num.length] = num[num[i] % num.length] + num.length;
}
for (int i = 0; i < num.length; i++) {
if (num[i] >= 2 * num.length) {
System.out.println(i);
}
}
}
public static void main(String[] args) {
int[] numbers = { 1, 0, 0, 4, 1 };
// for (int i = 0; i < numbers.length; i++) {
// numbers[i] += 1;
// }
System.out.println("The duplicate numbers are::");
findDuplicateNumber2(numbers);
}
}