-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathFindingDuplicateNumber.java
More file actions
32 lines (29 loc) · 1013 Bytes
/
Copy pathFindingDuplicateNumber.java
File metadata and controls
32 lines (29 loc) · 1013 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
import java.util.Scanner;
public class FindDuplicate { //{4,3,1,1,2} me [1,4] elements should only be there and since 1 is repeated it is our answer as per answer
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();//taking input of length of array by the user
int[] a = new int[n + 1];//taking input of array
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
System.out.println(duplicate(a));
static int duplicate(int[] a) {
int i = 0;
while (i < a.length) {
if (a[i] != i + 1) {
int correct = a[i] - 1;
if (a[i] != a[correct]) {
int temp = a[correct];
a[correct] = a[i];
a[i] = temp;
} else {
return a[i];
}
} else {
i++;
}
}
return -1;
}
}