Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions JAVA/DataStructures/Arrays/FindingDuplicateNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}