Here is the sample of code, which I got from here.
public class Snippet {
private static final int[] ARRAY = {1, 4, 3, 18, 2, 8, 9, 6, 5, 10,
11, 12, 13, 14, 15, 16, 17, 19, 0, 20};
//{1,2,4,5,6,8,7,9,3}
private int getMissingElem() {
int XOR = 0;
for (int i = 0; i < 20; i++) {
if (ARRAY[i] != 0) {
XOR ^= ARRAY[i];
}
XOR ^= (i + 1);
}
return XOR;
}
public static void main(String[] args) {
Snippet s = new Snippet();
System.out.println(s.getMissingElem());
}
}
I just came to know about how, XOR
have the values of missing elements of array.