You have been given a non-empty array of integers in which every element appears twice except for one. You have to Find that single one.
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
We will use XOR Operation to achieve this goal.
public class SingleNumber {
public static void main(String[] args) {
int[] arr = new int[] {12, 4, 36, 10, 12, 36, 4};
int res = arr[0];
for(int i=1;i<arr.length;i++) {
res = res ^ arr[i];
}
System.out.println(res);
}
}
Output :
10
Time Complexity of above solution os O(n) and Space Complexity is O(1) (i.e no extra space is used)



