Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
In a brute force approach, for each element in the array, we can check if it appears again in the array. We can do this by comparing each element against all others. The element that doesn't have a duplicate is the single number.
Utilize a HashMap to count the occurrences of each element. Then, iterate over the HashMap to find the element with a count of one, which is our single number.
The most optimal solution involves using the XOR operation.
XOR has three important properties:
Putting these together:
When we XOR all elements in the array, every duplicate pair cancels out to 0, and only the unique element survives.