Understanding single, double, and jodi numbers helps in result reading. Chart reference: 👉 https://dpboss-dpboss.com
seen from United States
seen from United States
seen from China
seen from Canada

seen from China

seen from Vietnam

seen from United States
seen from China

seen from United States
seen from United States
seen from United States

seen from United States

seen from Malaysia

seen from United States
seen from China

seen from Malaysia
seen from Morocco
seen from United States

seen from Singapore
seen from Iceland
Understanding single, double, and jodi numbers helps in result reading. Chart reference: 👉 https://dpboss-dpboss.com
1. Single Number
Single Number
Given an array of integers, every element appears twice except for one. Find that single one.
A straightforward solution is to scan the array and count each item until you find an item which is not counted twice. The space complexity will be O(n).
Another solution is to sort the array first, then do a scan to find an item which is not shown up twice. The best time complexity is O(nlogn).
In the note of the problem, it is said that there is a solution with constant memory complexity and linear time complexity.
The tricky part is that any integer a, doing twice XOR b, will still results a. No matter how many intermediate XOR operations have been done in between.
E.g. 3 XOR 5 XOR 3 = 5
So the solution is to do XOR operations on all values and the final value is the one which is not shown twice. The memory complexity is therefore O(1) and time complexity is O(n).
You can find my code here.