Unique numbers 3 test case 1 failed

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
int ans = 0;
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
if ((ans ^ a[i]) == 0) {
ans = ans ^ a[i];
}
ans = ans ^ a[i];
}
System.out.println(ans);

for test case
4
1 1 1 2
correct answer 2
debug for this