-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathfindAllDuplicates.java
More file actions
33 lines (30 loc) · 894 Bytes
/
findAllDuplicates.java
File metadata and controls
33 lines (30 loc) · 894 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class findAllDuplicates {
public static void main(String[] args) {
int[] arr = {1, 3, 2, 8, 4, 7, 6, 5, 4, 2, 8};
System.out.println(cycle(arr));
}
public static List<Integer> cycle(int[] arr) {
List<Integer> list = new ArrayList<>();
int i = 0;
while (i < arr.length) {
int correct = arr[i] - 1;
if (arr[i] != arr[correct]) {
swap(arr, i, correct);
} else i++;
}
for (int j = 0; j < arr.length; j++) {
if (arr[j] != j + 1) {
list.add(arr[j]);
}
}
return list;
}
public static void swap(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}