1305. Single Number I — One Element Appears Once

EasyBit ManipulationBit ManipulationXORArray

Given a non-empty array where every element appears exactly twice except for one element that appears once, find that single element. XOR of a value with itself is 0, so XOR-ing everything cancels the pairs.

Input: A JSON object {"nums": [<integers>]} where all but one value appear twice.

Output: Return the element that appears only once.

Examples

Example 1
Input: {"nums":[2,3,5,3,2]}
Output: 5
Explanation: Pairs 2 and 3 cancel, leaving 5.
Example 2
Input: {"nums":[7]}
Output: 7
Explanation: A single element is itself the answer -> 7.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →