I am trying to find element that is duplicated in an array. Program is intended to take user input.
Here is the code:
package sortingattempt;
import java.util.Scanner;
public class ArraySimilar {
public static void main(String args[]){
int[] a = new int[100];
int[] b = new int[5];
int Duplicate = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array");
int size = sc.nextInt();
//Scanner elem = new Scanner(System.in);
System.out.println("Input elements in array" +size);
for(int j = 0;j<size;j++){
a[j] = sc.nextInt();
}
System.out.println(a.length);
System.out.println("a[]" + a.toString());
for (int i = 0; i < a.length;i++){
b[0] = a[i];
if (b[0] == a[i+1]){
Duplicate = b[0];
System.out.println(Duplicate);
}
i++;
}
System.out.println("No common variable");
}
}
When I try to run it, its running properly till line where I ask user to give input. After entering input nothing is happening. Please indicate errors in program. Thank you.
sc.nextLine();beforea[j] = sc.nextInt();